【问题标题】:Device's default layout direction changes application's default layout direction when device language is changed设备的默认布局方向在更改设备语言时更改应用程序的默认布局方向
【发布时间】:2020-01-05 10:55:11
【问题描述】:

最近我收到了用户关于应用程序布局方向被打乱并且显示不正确的报告。

我在style.xml中做了所有方向ltr,但是有些用户改变了手机语言。结果,设备默认布局方向改变并导致应用程序方向反转。(但我不想)

ltr 更改为 rtl

rtl 更改为 ltr

我的假设是,如果我在 style.xml 中设置静态布局方向,布局永远不会改变它的方向,但它没有工作并且方向改变了。我必须做什么?如何防止布局方向改变?

【问题讨论】:

    标签: android orientation direction


    【解决方案1】:

    以下是在活动中强制 LTR 的方法。

    <item name="android:layoutDirection">ltr</item>
    

    例如:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:layoutDirection">ltr</item>
    </style>
    

    您也可以直接在布局中使用它,并在单个视图上组合不同的方向。

    android:layoutDirection="rtl"
    

    例如:

    <LinearLayout android:id="@+id/layout_linearlayout_rtl"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layoutDirection="rtl">
    
            <LinearLayout android:id="@+id/layout_linearlayout_rtl_child_1"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layoutDirection="ltr" />
    
            <LinearLayout android:id="@+id/layout_linearlayout_rtl_child_2"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layoutDirection="rtl" />
    
            <LinearLayout android:id="@+id/layout_linearlayout_rtl_child_3"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layoutDirection="inherit" />
    
            <LinearLayout android:id="@+id/layout_linearlayout_rtl_child_4"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layoutDirection="locale" />
    
    </LinearLayout>
    

    它可以从 API 17 开始使用。

    希望对你有帮助

    【讨论】:

    • 好,我会尽快测试它?
    • 这个工作ltr
    【解决方案2】:

    试试这个

     android:supportsRtl="false"
    

    &lt;application&gt;标签内的manifest.xml中设置这个

    【讨论】:

    • android:supportsRtl 默认为 false,还有一些使用 rtl 的 API,我不想忽略它们。
    猜你喜欢
    • 2012-06-20
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多