如何在展示活动作为一个自定义对话框的宽度和高度 [英] How to customize the width and height when show an Activity as a Dialog

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

问题描述

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

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

这篇关于如何在展示活动作为一个自定义对话框的宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆