Android 如何创建将在水平维度中充满的对话框
当我在布局中使用时,它指定了这个对话框Android 如何创建将在水平维度中充满的对话框,android,layout,dialog,Android,Layout,Dialog,当我在布局中使用时,它指定了这个对话框android:layout\u width=“match\u parent”我得到这个对话框: 我需要更广泛的对话。有什么想法吗?您可以抓取对话框使用的窗口对象,然后重置宽度。下面是一个简单的例子: //show the dialog first AlertDialog dialog = new AlertDialog.Builder(this) .setTitle("Test Dialog") .setMessage("
android:layout\u width=“match\u parent”
我得到这个对话框:
我需要更广泛的对话。有什么想法吗?您可以抓取对话框使用的
窗口
对象,然后重置宽度。下面是一个简单的例子:
//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);
这是最终结果。请注意,如果需要微调(如更改背景等),可以对对话框进行更多的样式设置。当我过去不得不这样做时,我通常使用此方法,并在builder类上使用setView()
自定义对话框中使用的布局
解决此问题的另一种方法是使用:
import android.support.v7.app.AlertDialog;
而不是:
import android.app.AlertDialog;
发布xml布局代码&尝试填充父值而不是匹配parent@rajpara
fill\u parent
和match\u parent
做的事情完全相同(都是值常量-1
),请记住,这在大屏幕上可能看起来很奇怪,比如平板电脑。如果我的类扩展了应用程序,我想在其中调用alertdialog怎么办?如果使用DialogFragment,那么设置必须在onStart
方法中完成,请看一个问题:为什么一开始就这么痛苦?如果XML的外部布局状态为“match_parent”,那么应该是这样的!该死的,谷歌。@h_k你可以通过调用dialog.getWindow().setBackgroundDrawable(新的ColorDrawable(android.graphics.Color.TRANSPARENT)),删除那些边缘代码>最简单、最快、最好的解决方案。这应该是公认的答案。