Android AlertDialog显示横向大小的视图,方向为横向
我的应用程序的主要活动在清单中设置为始终处于纵向视图中。当我将应用程序加载到平板电脑上时,这一切正常 但是,当我使用菜单按钮并单击“设置”时,会打开一个膨胀xml布局的Android AlertDialog显示横向大小的视图,方向为横向,android,orientation,android-alertdialog,landscape-portrait,Android,Orientation,Android Alertdialog,Landscape Portrait,我的应用程序的主要活动在清单中设置为始终处于纵向视图中。当我将应用程序加载到平板电脑上时,这一切正常 但是,当我使用菜单按钮并单击“设置”时,会打开一个膨胀xml布局的AlertDialog,平板电脑将显示整个对话框的左2/3左右。剩下的部分在屏幕右侧。只有当我在平板电脑上以横向模式安装应用程序时,或者在应用程序未运行时将平板电脑转到横向模式时(即使我返回到纵向并单击应用程序),才会发生这种情况。该对话框甚至调用this.setRequestedOrientation(ActivityInfo.
AlertDialog
,平板电脑将显示整个对话框的左2/3左右。剩下的部分在屏幕右侧。只有当我在平板电脑上以横向模式安装应用程序时,或者在应用程序未运行时将平板电脑转到横向模式时(即使我返回到纵向并单击应用程序),才会发生这种情况。该对话框甚至调用this.setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u Grait)
以确保它保持纵向(即使此调用是否存在问题)
换句话说,AlertDialog
在纵向视图中的显示方式是,它正在放大横向视图(因此其中一些视图会脱离屏幕),即使AlertDialog
的实际方向是纵向的,就像它应该的那样
(如果上述措辞令人困惑,我深表歉意-如果需要,请让我澄清。)
那么平板电脑为什么要这么做呢?我已经在一些安卓手机上进行了测试,但没有出现这种情况,所以我不明白为什么平板电脑会这样做
旁注:
- 这甚至不只是在这个
中发生的。。如果以横向模式启动应用程序,则在应用程序启动时显示的我的EULA(另一个AlertDialog
)也会以这种方式显示警报对话框
- 我甚至允许通过取消所有指定纵向/横向模式的调用来实现横向的可用性,并且平板电脑在只有平板电脑(而不是手机)的纵向视图中显示时,仍在屏幕外扩展对话框
showDialog(EXAMPLE_CASE);
被调用函数调用的函数:
protected Dialog onCreateDialog(final int id) {
Dialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
Display display = getWindowManager().getDefaultDisplay();
int mwidth = display.getWidth();
int mheight = display.getHeight();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
switch(id) {
// Example of what all my cases look like (there were way too many to copy)
case EXAMPLE_CASE:
builder.setTitle("Example")
.setMessage("Example message")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface,int i) {
dialoginterface.dismiss();
showDialog(DIALOG_CHOICE);
}
})
.setCancelable(false);
dialog = builder.create();
break;
}
if (dialog != null) {
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = mwidth;
lp.height = mheight;
lp.x = mwidth;
//lp.y = mheight;
lp.dimAmount=0.0f;
//dialog.show();
dialog.getWindow().setAttributes(lp);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
removeDialog(id);
}
});
}
return dialog;
}
您可以尝试获得如下屏幕尺寸:
Display display = getWindowManager().getDefaultDisplay();
int mwidth = display.getWidth();
int mheight = display.getHeight();
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.)
d.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = mwidth;
lp.height = myheight;
//change position of window on screen
lp.x = mwidth/2; //set these values to what work for you; probably like I have here at
lp.y = mheight/2; //half the screen width and height so it is in center
//set the dim level of the background
lp.dimAmount=0.1f; //change this value for more or less dimming
d.getWindow().setAttributes(lp);
//add a blur/dim flags
d.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND | WindowManager.LayoutParams.FLAG_DIM_BEHIND);
然后更改对话框
,如下所示:
Display display = getWindowManager().getDefaultDisplay();
int mwidth = display.getWidth();
int mheight = display.getHeight();
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.)
d.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = mwidth;
lp.height = myheight;
//change position of window on screen
lp.x = mwidth/2; //set these values to what work for you; probably like I have here at
lp.y = mheight/2; //half the screen width and height so it is in center
//set the dim level of the background
lp.dimAmount=0.1f; //change this value for more or less dimming
d.getWindow().setAttributes(lp);
//add a blur/dim flags
d.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND | WindowManager.LayoutParams.FLAG_DIM_BEHIND);
此外,您还说您正在膨胀自定义布局。您是否尝试过修改该布局中视图的
布局高度
和布局宽度
,看看这是否会产生影响?除了下面的回答,您还有其他解决方案吗