【发布时间】:2012-02-24 12:48:11
【问题描述】:
我的应用程序的主要活动在清单中设置为始终处于纵向视图中。当我将应用程序加载到平板电脑上时,这可以正常工作。
但是,当我使用菜单按钮并单击“设置”时,会打开一个扩展 xml 布局的AlertDialog,平板电脑将显示整个对话框的左侧 2/3 左右。其余部分在屏幕外向右移动。仅当我在平板电脑上安装我的应用程序时将其保持在横向模式,或者如果我在应用程序未运行时将平板电脑切换到横向模式(即使我返回纵向并单击应用程序),才会出现这种情况。该对话框甚至调用this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 以确保它保持纵向(即使此调用仍然存在问题)。
换句话说,AlertDialog 在纵向视图中显示的方式是放大横向视图(因此其中一些会离开屏幕),即使 AlertDialog 的实际方向是纵向的,就像它应该的那样。
(如果上述措辞令人困惑,我深表歉意 - 如果需要,请让我澄清任何事情。)
那么平板电脑为什么要这样做呢?我在几部安卓手机上测试过都没有,所以不明白平板为什么会这样。
旁注:
这甚至不会发生在这个
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;
}
【问题讨论】:
-
除了下面回答之外,您还有其他解决方案吗
标签: android orientation android-alertdialog landscape-portrait