我的应用程序的主要活动在清单中设置为始终处于纵向视图。当我将应用程序加载到平板电脑上时,这可以正常工作。

但是,当我使用菜单按钮并单击"设置"时,将打开一个AlertDialog来放大xml布局,平板电脑将显示整个对话框的左2/3左右。其余部分从屏幕外移到右侧。仅当我在横向模式下将应用程序安装在平板电脑上或在应用程序未运行时将平板电脑切换为横向模式时,才会发生这种情况(即使我返回到"纵向"并单击该应用程序)。对话框甚至调用this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);以确保它保持纵向(即使此调用是否仍然存在问题)。

换句话说,AlertDialog在纵向视图中的显示方式是,它炸开了风景视图(因此其中有些不在屏幕上),即使AlertDialog的实际方向是纵向的,就像它一样应该。

(如果上述措词令人困惑,我深表歉意,请我澄清任何需要的内容。)

那么平板电脑为什么要这样做呢?我已经在一些Android手机上对其进行了测试,但是这种情况并没有发生,因此我不明白平板电脑为什么会这样做。

旁注:

甚至对于此AlertDialog都不会发生这种情况。.如果我以横向模式启动应用程序,则我在应用程序开头显示的EULA(另一个AlertDialog)也会以这种方式出现。

通过消除所有用于指定纵向/横向模式的呼叫,我什至允许横向使用,并且平板电脑仅在纵向视图中(而不是在手机上)处于纵向视图时,仍然在屏幕外扩展对话框。

编辑:

该代码在手机上运行良好,但平板电脑问题仍然存在。如果我在下面取消注释dialog.show();,则平板电脑和手机会显示我想要的尺寸,但会显示为黑色,而不是变暗的主屏幕。有任何想法吗?

调用函数执行此操作:

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();

然后像这样修改Dialog:

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);

另外,您说您正在夸大自定义布局。 您是否尝试过修改布局中视图的layout_height和layout_width来查看是否有区别?