在我的项目中,我必须为layout-ldltr
和layout-ldrtl
语言创建Right-To-Left
和Left-To-Right
布局。
我启动了从右到左语言的应用程序,一切都很好。但是,当方向更改时,尽管当前layout-ldltr
设置为RTL语言,但android相对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>
UPDATE
在@JohanShogun发表评论后,我将android:layoutDirection="rtl"
更改为android:layoutDirection="locale"
,并且大多数项目已解决问题。
在第一张照片中,所有元素都显示得很好:
在横向模式下,位于屏幕顶部的标题项中的StickyListHeader list view中,Android从ltr布局使用!:
如果您的文件夹中有layout-land-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
开始左端和end右]],如果用户的语言环境为rtl
start右端和end左] >。rtl
和ltr
编写的布局文件,而不是使用单独的布局文件,如果保留不同的文件,很容易错过一个版本的更新,从而导致糟糕的用户体验。] >"-ldrtl"
,然后将所有android:layoutDirection="rtl"
更改为android:layoutDirection="locale"
。