如何在Android中防止反向横向但允许横向



我有一个"活动",在任何情况下,我都希望视图位于设备的左侧(音量/电源按钮的对面(。

我在"活动"的纵向和横向方向上执行此操作没有问题,但当它转到反向横向和反向纵向时,视图将转到设备的右侧(音量/电源按钮一侧(。

是否可以允许屏幕方向更改但阻止反向方向?

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<CustomView
android:layout_width="150dp"
android:layout_height="wrap_content"
>
</CustomView>
<include
layout="@layout/some_layout"
>
</include>
</LinearLayout>

activity_main.xml(land(:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<CustomView
android:layout_width="wrap_content"
android:layout_height="150dp">
</CustomView>
<include layout="@layout/some_Other_layout" />
</LinearLayout>

我尝试过orientationListener,每当它反转为横向时,我都会尝试将方向设置为横向,然后通过设置未指定的方向来释放方向,但这不起作用,因为屏幕被锁定,方向的进一步变化没有被检测到。

关于如何解决这个问题,有什么想法吗?

您可以防止反向景观,如下Kotlin代码

requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

您可以使用以下Java代码

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

最新更新