如何创建将在水平维度上填充的对话框 [英] How to create dialog which will be full in horizontal dimension

查看:151
本文介绍了如何创建将在水平维度上填充的对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在布局中使用它指定这个对话框 android:layout_width =match_parent我得到这个对话框:





我需要对话框会更广泛任何想法?

解决方案

您可以通过抓取 Window 对话框使用,并重置宽度。以下是一个简单的例子:

  //首先显示对话框
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle(Test Dialog)
.setMessage(This should expand to the full width)
.show();
//抓住对话框的窗口,并更改宽度
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
窗口窗口= dialog.getWindow();
lp.copyFrom(window.getAttributes());
//这使得对话框占据了整个宽度
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);

这是最终结果。请注意,如果您需要微调事物(如更改背景等),则可以在对话框中执行更多样式。当我以前不得不这样做时,我通常会使用这个方法,并在构建器类中使用 setView()自定义对话框中使用的布局。 p>


When I use in layout, which specifies this dialog android:layout_width="match_parent" I get this dialog:

I need dialog which will be wider. Any ideas?

解决方案

You can do that by grabbing the Window object that the dialog uses, and resetting the width. Here's a simple example:

//show the dialog first
AlertDialog dialog = new AlertDialog.Builder(this)
        .setTitle("Test Dialog")
        .setMessage("This should expand to the full width")
        .show();
//Grab the window of the dialog, and change the width
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = dialog.getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);

Here's the end result. Note that there's a lot more styling you can do to the dialog if you need to fine tune things (like change the background, etc). When I've had to do this in the past, I usually use this method, and customize the layout used in the dialog with setView() on the builder class.

这篇关于如何创建将在水平维度上填充的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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