首页 > 解决方案 > 如何防止反向横向但在Android中允许横向?

问题描述

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

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

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

活动主.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(土地):

<?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,每当它进入反向横向时,我都会尝试将方向设置为横向,然后通过设置未指定来释放方向,但这不起作用,因为屏幕被锁定并且方向的进一步变化没有被检测到。

关于如何解决这个问题的任何想法?

标签: androidandroid-layoutandroid-orientation

解决方案


推荐阅读