我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。
我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。
我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。
我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。
当前回答
如果你正在使用约束布局,你可以在其中设置任何视图,以填充屏幕的百分比:
layout_constraintWidth_percent = " 0.8 "
例如,如果你在对话框中有一个ScrollView你想把它设置为屏幕高度的一个百分比。它是这样的:
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.8">
希望它能帮助到一些人!!
其他回答
根据Android平台开发人员Dianne Hackborn在这个讨论组的帖子,对话框将窗口的顶层布局宽度和高度设置为WRAP_CONTENT。要使对话框更大,可以将这些参数设置为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);
注意,属性是在对话框显示之后设置的。系统对设置时间很挑剔。(我猜布局引擎必须在对话框第一次显示时设置它们。)
最好通过扩展Theme来实现这一点。对话,那么您就不必玩什么时候调用setAttributes的猜谜游戏了。(尽管让对话框自动采用适当的明暗主题或蜂巢Holo主题需要更多的工作。这可以根据http://developer.android.com/guide/topics/ui/themes.html#SelectATheme来做)
这里有一个简短的答案,对我来说是有效的(在API 8和API 19上测试)。
Dialog mDialog;
View mDialogView;
...
// Get height
int height = mDialog.getWindow()
.getWindowManager().getDefaultDisplay()
.getHeight();
// Set your desired padding (here 90%)
int padding = height - (int)(height*0.9f);
// Apply it to the Dialog
mDialogView.setPadding(
// padding left
0,
// padding top (90%)
padding,
// padding right
0,
// padding bottom (90%)
padding);
尝试将你的自定义对话框布局包装成RelativeLayout而不是LinearLayout。这对我很管用。
public static WindowManager.LayoutParams setDialogLayoutParams(Activity activity, Dialog dialog)
{
try
{
Display display = activity.getWindowManager().getDefaultDisplay();
Point screenSize = new Point();
display.getSize(screenSize);
int width = screenSize.x;
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(dialog.getWindow().getAttributes());
layoutParams.width = (int) (width - (width * 0.07) );
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
return layoutParams;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
你必须在显示之前设置对话框的高度和宽度(dialog.show())
所以,像这样做:
dialog.getWindow().setLayout(width, height);
//then
dialog.show()