AlertDialog在横向中显示具有方向的横向视图

Mxy*_*xyk 2 android orientation landscape-portrait android-alertdialog

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

但是,当我使用我的菜单按钮并单击"设置"(打开一个AlertDialog膨胀xml布局)时,平板电脑将显示整个对话框的左2/3左右.其余的都在屏幕右侧.如果我在平板电脑上安装我的应用程序,同时在应用程序未运行时将平板电脑转为横向模式(即使我返回到Portrait并单击应用程序),也只会出现这种情况.该对话框甚至调用this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);以确保它保持纵向(即使此调用问题仍然存在).

换句话说,AlertDialog在纵向视图中显示的方式是它正在炸毁一个横向视图(因此它的一些在屏幕外),即使它的实际方向AlertDialog是肖像,就像它应该的那样.

(如果上面的措辞令人困惑,我很抱歉 - 如果需要,请我澄清一下.)

那么为什么平板电脑会这样做呢?我已经在一些Android手机上测试了这种情况并没有发生,所以我不明白平板电脑为什么会这样做.

侧面说明:

  • 这甚至都不会发生这种AlertDialog情况.AlertDialog如果我以横向模式启动应用程序,那么在应用程序开始时显示的另一个EULA(另一个)也会出现这种情况.

  • 我甚至通过删除指定纵向/横向模式的所有调用来允许景观的可用性,并且平板电脑在纵向视图中仅在平板电脑上而不是电话时仍在屏幕外扩展对话框.

编辑:

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

调用函数执行此操作:

showDialog(EXAMPLE_CASE);
Run Code Online (Sandbox Code Playgroud)

调用函数调用的函数:

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;
}
Run Code Online (Sandbox Code Playgroud)

koo*_*ng3 7

您可以尝试获取这样的屏幕尺寸:

Display display = getWindowManager().getDefaultDisplay(); 
int mwidth = display.getWidth();
int mheight = display.getHeight();
Run Code Online (Sandbox Code Playgroud)

然后改变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);
Run Code Online (Sandbox Code Playgroud)

此外,您说您正在膨胀自定义布局.您是否尝试过修改该布局中的视图layout_height以及是否有所作为layout_width