如何在展示活动作为一个自定义对话框的宽度和高度自定义、作为一个、对话框、宽度

2023-09-06 13:31:47 作者:男神霸哥

如果我已经定义了一个活动:

If I have defined a Activity:

public class DialogActivity extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(dialog_activity.xml);
    }


}

我想显示一个类似的对话框上面的活动,所以在的的Andr​​oidManifest.xml 的文件,我宣布本次活动象下面这样:

I would like to display the above activity like a dialog, so in the AndroidManifest.xml file, I declare this activity like below:

<activity android:name=".DialogActivity" android:theme="@android:style/Theme.Dialog"/>

一切都很好,在这一点上,我的 DialogActivity 表现为一个对话框。

Everything is fine at this point, my DialogActivity showed as a dialog.

现在的问题是如何定义的宽度和高度 DialogActivity 来使它更像一个小对话框? (目前,它占据了大部分屏幕默认情况下)

The problem is How to customize the width and height of the DialogActivity to make it more like a small dialog? (Currently it occupies most of the screen by default)

----------------------------更新--------------- --------

我定义的自定义主题,如下图所示:

I defined a custom theme like below:

<style name="myDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>

        <item name="android:width">100dip</item> 
        <item name="android:height">100dip</item>

</style>

然后,声明 DialogActivity 中的的的Andr​​oidManifest.xml 的作为。

Then, declare the DialogActivity in AndroidManifest.xml as.

<activity android:name=".DialogActivity" android:theme="@style/myDialog"/>

我的 DialogActivity 现在甚至占据了整个屏幕:(在宽度和高定义:

My DialogActivity now even occupies the whole screen:( . The width and height definition:

<item name="android:width">100dip</item>

在主题不采取任何效果,为什么呢?

in the theme do not take any effect, why?

推荐答案

我终于想通了自定义的大小我的 DialogActivity 的一种方式。

I finally figured out one way to customize the size of my DialogActivity.

这是 DialogActivity 的onCreate()方法,添加以下code里面的:

That's inside the onCreate() method of DialogActivity, add the following code:

WindowManager.LayoutParams params = getWindow().getAttributes();  
params.x = -20;  
params.height = 100;  
params.width = 550;  
params.y = -10;  

this.getWindow().setAttributes(params);