AlertDialog 显示横向大小的视图,方向为横向

发布于 2025-01-08 15:02:08 字数 2770 浏览 0 评论 0原文

我的应用程序的主要活动在清单中设置为始终处于纵向视图。当我将应用程序加载到平板电脑上时,效果很好。

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

换句话说,AlertDialog 在纵向视图中显示的方式是它会放大横向视图(因此其中一些会超出屏幕),即使 的实际方向也是如此。 AlertDialog 是纵向的,就像它应该的那样。

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

那么为什么平板电脑会这样做呢?我在几款安卓手机上测试过,都没有出现这种情况,所以我不明白为什么平板电脑会这样做。

旁注:

  • 仅此 AlertDialog 也不会发生这种情况。我的 EULA 显示在应用程序的启动处(另一个 AlertDialog< /code>) 如果我以横向模式启动应用程序,也会以这种方式出现。

  • 我什至通过取消所有指定纵向/横向模式的调用来允许横向的可用性,并且当仅在平板电脑上而不是手机上以纵向视图保持时,平板电脑仍然将对话框扩展至屏幕外.

编辑:

此代码在手机上运行良好,但平板电脑问题仍然存在。如果我取消下面的 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;
}

My app's main activity is set in the manifest to always be in portrait view. This works fine when I load the app onto the tablet.

However, when I use my menu button and click "Setup", which opens an AlertDialog that inflates an xml layout, the tablet will display the left 2/3 or so of the entire dialog. The rest of it goes offscreen to the right. This case only occurs if I install my app on the tablet while holding it in landscape mode or if I turn the tablet to landscape mode while the app is not running (even if I go back to Portrait and click on the app). The dialog even calls this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); to make sure it stays in portrait (even though the problem persists with this call or not).

In other words, the way the AlertDialog is being displayed in portrait view is that it is blowing up a landscape view (and thus some of it goes offscreen), even though the actual orientation of the AlertDialog is portrait, like it should be.

(I apologize if the wording above was confusing - ask me to clarify anything if needed.)

So why would the tablet do this? I've tested it on a few android mobile phones and this doesn't happen, so I don't understand why the tablet will do this.

SIDE NOTES:

  • This isn't even occurring for just this AlertDialog either.. my EULA that displays at the start of the app (another AlertDialog) also appears this way if I start the app in landscape mode.

  • I've even allowed the usability of landscape by getting rid of all calls to specify portrait/landscape mode, and the tablet is still expending the dialog off-screen when held in portrait view on just the tablet, but not the phones.

EDIT:

This code works well on the phone, but the tablet problem still persists. If I uncomment dialog.show(); below, the tablet and phone display the dimensions I want, but on blackness instead of the dimmed main screen. Any ideas?

Calling function does this:

showDialog(EXAMPLE_CASE);

Function that gets called by the calling function:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两人的回忆 2025-01-15 15:02:08

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

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_heightlayout_width 来看看是否有所不同?

You could try getting the screen dimensions like this:

Display display = getWindowManager().getDefaultDisplay(); 
int mwidth = display.getWidth();
int mheight = display.getHeight();

And then alter the Dialog like this:

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

Also, you say you are inflating a custom layout. Have you tried tinkering with the layout_height and layout_width of the views in that layout to see if that makes a difference?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文