如何使警报对话框填充 90% 的屏幕尺寸? [英] How to make an alert dialog fill 90% of screen size?

查看:11
本文介绍了如何使警报对话框填充 90% 的屏幕尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以很好地创建和显示自定义警报对话框,但即便如此,我在对话框 xml 中有 android:layout_width/height="fill_parent" 它也只有内容一样大.

I can create and display a custom alert dialog just fine but even so I have android:layout_width/height="fill_parent" in the dialog xml it is only as big as the contents.

我想要的是填充整个屏幕的对话框,除了 20 像素的填充.然后,作为对话框一部分的图像将使用 fill_parent 自动拉伸到整个对话框大小.

What I want is dialog that fills the entire screen except maybe a padding of 20 pixel. Then the image that is part of the dialog would automatically stretch to the full dialog size with fill_parent.

推荐答案

根据 Android 平台开发者 Dianne Hackborn 在 this 讨论组帖子,Dialogs 将其 Window 的顶级布局宽度和高度设置为 WRAP_CONTENT.要使 Dialog 更大,您可以将这些参数设置为 MATCH_PARENT.

According to Android platform developer Dianne Hackborn in this discussion group post, Dialogs set their Window's top level layout width and height to WRAP_CONTENT. To make the Dialog bigger, you can set those parameters to MATCH_PARENT.

演示代码:

    AlertDialog.Builder adb = new AlertDialog.Builder(this);
    Dialog d = adb.setView(new View(this)).create();
    // (That new View is just there to have something inside the dialog that can grow big enough to cover the whole screen.)

    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(d.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.MATCH_PARENT;
    d.show();
    d.getWindow().setAttributes(lp);

请注意,属性是在对话框显示后设置的.系统对它们何时设置很挑剔.(我猜布局引擎必须在第一次显示对话框时设置它们,或者其他什么.)

Note that the attributes are set after the Dialog is shown. The system is finicky about when they are set. (I guess that the layout engine must set them the first time the dialog is shown, or something.)

最好通过扩展 Theme.Dialog 来做到这一点,这样您就不必玩什么时候调用 setAttributes 的猜谜游戏了.(虽然让对话框自动采用适当的浅色或深色主题或 Honeycomb Holo 主题需要更多的工作.这可以根据 http://developer.android.com/guide/topics/ui/themes.html#SelectATheme)

It would be better to do this by extending Theme.Dialog, then you wouldn't have to play a guessing game about when to call setAttributes. (Although it's a bit more work to have the dialog automatically adopt an appropriate light or dark theme, or the Honeycomb Holo theme. That can be done according to http://developer.android.com/guide/topics/ui/themes.html#SelectATheme )

这篇关于如何使警报对话框填充 90% 的屏幕尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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