【问题标题】:Is android bug in load "layout-ldltr" layout against "layout-ldrtl" after orientation changed?方向改变后加载“layout-ldltr”布局中的android错误是否针对“layout-ldrtl”?
【发布时间】:2015-09-30 04:48:09
【问题描述】:

在我的项目中,我必须为 Right-To-LeftLeft-To-Right 语言创建 layout-ldltrlayout-ldrtl 布局。

我启动了从右到左语言的应用程序,一切都很好。 但是当方向改变时,android会根据layout-ldrtl加载layout-ldltr布局,尽管当前Locale设置为RTL语言!!!

如何解决这个问题?

AndroidManifest.xml

<application
    android:name=".QuranApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

layout-ldrtl\activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layoutDirection="rtl">

<include layout="@layout/toolbar" />

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:layoutDirection="rtl">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layoutDirection="rtl"/>

    <include
        layout="@layout/list_view"
        android:layout_width="@dimen/drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>

更新

@JohanShogun 发表评论后,我将android:layoutDirection="rtl" 更改为android:layoutDirection="locale",大多数项目的问题都解决了。

在第一张照片中,所有元素都显示得非常好:

在横向模式下,StickyListHeader list view 中的标题项中粘贴在屏幕顶部,Android 使用 ltr 布局!:

【问题讨论】:

    标签: android android-layout android-5.0-lollipop right-to-left left-to-right


    【解决方案1】:

    如果您有一个带有 layout-land 的文件夹,它将接管 layout-ldrtl 等。您可能需要为 layout-land-ldrtl 等创建文件夹。

    处理从右到左和从左到右布局的另一种方法是保留一个布局文件,该文件编写为适用于两个版本。

    在 LinearLayout 等上有一个属性:

    android:layoutDirection
    

    您可以将其设置为“Locale”的值,您的水平布局将翻转顺序。

    不要使用align_left/align_rightgravity_left/gravity_right,而是使用align_start/align_endgravity_start/gravity_end(适用于所有left/right 属性)。开始和结束标签取决于布局方向。如果用户的语言环境为ltrstart 为左end 为右,如果用户的语言环境为rtlstart 为右 em> 和 end 在左边

    我个人更喜欢使用为rtlltr 编写的布局文件,而不是使用单独的布局文件,如果您保留不同的文件,很容易错过一个版本中的更新,从而导致糟糕的用户体验。

    还将您的布局移动到布局文件夹(删除"-ldrtl"),然后将所有android:layoutDirection="rtl" 更改为android:layoutDirection="locale"

    【讨论】:

    • 感谢您的回复。我添加了 android:layoutDirection 属性,但不影响这个问题。
    • 如果您使用 android 布局属性,您可能希望跳过包含 rtl/ltr 资源的文件夹。另请显示您的代码以及您如何使用该属性
    • 还记得把它放在你的应用程序清单中:android:supportsRtl="true"(进入应用程序级别)developer.android.com/intl/es/guide/topics/manifest/…
    • 试试这个:将布局移动到布局文件夹(删除“-ldrtl”),然后将所有 android:layoutDirection="rtl" 更改为 android:layoutDirection="locale"
    • 更新了答案,但我不确定您是否还有更多问题?您可能需要考虑对横向布局进行相同的更改。粘性标题项可能没有 rtl 支持。如果是这样,您可以通过修改它的资源文件来轻松添加它。
    猜你喜欢
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 2016-07-18
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    相关资源
    最近更新 更多