始终横向查看对话框

发布于 2024-10-07 00:30:02 字数 1896 浏览 3 评论 0原文

我正在重复我在 Google 群组中的 android 群组中发表的帖子 (http://groups.google.com/group/android-developers/browse_thread/thread/78d0b7496a51e3b7# ),希望我在这里能有一些运气!

我有一个显示项目列表的活动。单击某个项目后, 我显示一个带有一些按钮和图像的全屏对话框。这 横向看起来不错,但纵向看起来不太好(由于 图像长宽比)。我想知道是否有可能 无论当前屏幕如何,始终横向显示此对话框 方向。对话结束后,我将移交 方向回到传感器。到目前为止,这是我所拥有的:

OnItemClickListener listlistener = new OnItemClickListener() { 
                @Override 
                public void onItemClick(AdapterView parent, View arg1, int position, 
                                long arg3) { 
                        final Dialog dialog = new Dialog(getParent()); 
                        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
                        final int orientation = 
getResources().getConfiguration().orientation; 
                        if (orientation == 1){ 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
                        } 
                        dialog.setContentView(R.layout.dialog); 
                        dialog.setCancelable(true); 
                        ImageView img = (ImageView) dialog.findViewById(R.id.img); //A 
static image for testing UI 
                        //BUTTON LISTENERS HERE 
                        dialog.show(); 
                        dialog.setOnCancelListener(new OnCancelListener() { 
                                @Override 
                                public void onCancel(DialogInterface dialog) { 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); 
                                } 
                        }); 
                } 
        }; 

但是通过这样做,屏幕首先改变方向,然后尝试 显示对话框。但是在方向改变时屏幕是 重新启动活动,但我的对话框未显示。有没有更简单的 方法来做到这一点?我可以为对话框使用两种不同的布局吗 根据方向加载相应的?如果是这样,那么 纵向模式的 XML 是什么样子的? 谢谢!

I'm repeating a post I've made at the android group in google groups (http://groups.google.com/group/android-developers/browse_thread/thread/78d0b7496a51e3b7# ), hopefully I'll have some luck here!

I have an activity displaying a list of items. Upon clicking an item,
I display a full-screen dialog with some buttons and an image. This
looks great in landscape, but doesn't look good in portrait (Due to
the image aspect ratio). I would like to know if its possible to
always display this dialog in landscape irrespective of current screen
orientation. And after the dialog is dismissed, I'll handover the
orientation back to the sensor. Here's what I have so far:

OnItemClickListener listlistener = new OnItemClickListener() { 
                @Override 
                public void onItemClick(AdapterView parent, View arg1, int position, 
                                long arg3) { 
                        final Dialog dialog = new Dialog(getParent()); 
                        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
                        final int orientation = 
getResources().getConfiguration().orientation; 
                        if (orientation == 1){ 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
                        } 
                        dialog.setContentView(R.layout.dialog); 
                        dialog.setCancelable(true); 
                        ImageView img = (ImageView) dialog.findViewById(R.id.img); //A 
static image for testing UI 
                        //BUTTON LISTENERS HERE 
                        dialog.show(); 
                        dialog.setOnCancelListener(new OnCancelListener() { 
                                @Override 
                                public void onCancel(DialogInterface dialog) { 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); 
                                } 
                        }); 
                } 
        }; 

But by doing so, the screen changes orientation first and then tries
to display the dialog. But on orientation change the screen is
restarting the activity and my dialog isn't shown. Is there a simpler
way to do this? Could I use two different layout for the dialog and
load a corresponding one depending on the orientation? If so, then
what would the XML for the portrait mode look like?
Thanks!

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

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

发布评论

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

评论(2

我ぃ本無心為│何有愛 2024-10-14 00:30:02

如果您为对话框使用自定义布局,只需设计一个新的布局并将其放入layout-land文件夹中(如果需要,请创建它),这样它就会自动加载相应的布局。

If you are using a custom layout for the dialog, just design a new one and put it in the layout-land folder (create it if necesary), that way it will load the corresponding one automatically.

停顿的约定 2024-10-14 00:30:02

您还可以旋转顶视图
android:rotation="90" XML

constraintNumber.rotation = 90f以编程方式

那么你就不需要改变活动方向,这可能会导致一些其他问题。

如果您的屏幕有背景,则可以将背景添加到顶部视图组,然后将旋转添加到子视图组。因为旋转也适用于背景。

例如:

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/rectangle_blue_24dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:rotation="90"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">

<!--          Your views-->
            
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>

You can also rotate your top view
android:rotation="90" in XML
or
constraintNumber.rotation = 90f programmatically.

Then you don't need to change activity orientation, which can lead to some other issues.

If you have a background for your screen then you can add background to the top view group and then add rotation for rotation to the child view group. Because the rotation will work for the background as well.

For example:

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/rectangle_blue_24dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:rotation="90"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">

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