在方向改变后,Android中的“layout-ldltr”布局与“layout-ldrtl”布局加载存在问题吗?

10
在我的项目中,我需要为从右到左和从左到右的语言分别创建layout-ldltrlayout-ldrtl布局。当应用程序以从右到左的语言启动时,一切都很好。但是当方向改变时,Android加载layout-ldltr布局而不是layout-ldrtl,尽管当前Locale已设置为从右到左的语言!!!如何解决这个问题?请在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列表视图中位于屏幕顶部的标题项上,Android使用了ltr布局!:

横屏方向

1个回答

6
如果你有一个名为layout-land的文件夹,它将优先于layout-ldrtl等文件夹,你可能需要创建layout-land-ldrtl等文件夹。
处理从右到左和从左到右布局的另一种方法是保留一个适用于两个版本的布局文件。
在LinearLayout等控件中,有一个属性:
android:layoutDirection

您可以将其设置为“Locale”的值,您的水平布局将翻转顺序。 不要使用align_left/align_right和gravity_left/gravity_right,改用align_start/align_end和gravity_start/gravity_end(适用于所有left/right属性)。开始和结束标签取决于布局方向。如果用户的语言环境是ltr,则start是left,end是right;如果用户的语言环境是rtl,则start是right,end是left。 我个人更喜欢使用既适用于rtl又适用于ltr的布局文件,而不是使用不同的文件。如果保留不同的文件,则很容易忽略一个版本中的更新,导致用户体验不佳。 还需要将布局移动到layout文件夹中(删除“-ldrtl”),然后将所有android:layoutDirection="rtl"更改为android:layoutDirection="locale"。

谢谢您的回复。我添加了android:layoutDirection属性,但对于这个问题没有影响。 - Sayed Abolfazl Fatemi
请在应用程序清单中添加以下内容: android:supportsRtl="true"(放在应用程序级别)http://developer.android.com/intl/zh-cn/guide/topics/manifest/application-element.html 请注意,这个内容只是一个参考链接。 - JohanShogun
我编辑了问题并添加了我的代码,以便您提出建议。 - Sayed Abolfazl Fatemi
1
尝试这样做:将您的布局移动到布局文件夹中(删除“-ldrtl”),然后将所有android:layoutDirection =“rtl”更改为android:layoutDirection =“locale”。 - JohanShogun
1
更新了答案,不过我不确定你是否还有其他问题?你可能需要考虑对横向布局进行相同的更改。粘性标题项可能没有从右到左的支持。如果是这样,你可以通过修改其资源文件来很容易地添加它。 - JohanShogun
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接