将活动显示为对话框时如何自定义宽度和高度

How to customize the width and height when show an Activity as a Dialog(将活动显示为对话框时如何自定义宽度和高度)

本文介绍了将活动显示为对话框时如何自定义宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我定义了一个活动:

If I have defined a Activity:

public class DialogActivity extends Activity{

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


}

我想像对话框一样显示上面的活动,所以在 AndroidManifest.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>

然后,将 AndroidManifest.xml 中的 DialogActivity 声明为.

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

我的 DialogActivity 现在甚至占据了整个屏幕:( . widthheight 定义:

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.

DialogActivityonCreate()方法里面,添加如下代码:

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); 

这篇关于将活动显示为对话框时如何自定义宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将活动显示为对话框时如何自定义宽度和高度

基础教程推荐