如何RTL这个Android布局?

Mok*_*adi 3 android android-layout right-to-left

我想在Android中布局下面的rtl(从右到左)就像layoutDirection="rtl"在父布局中使用时(但它仅用于4.2及更高版本)以及任何方式使其明确地从右向左.这是主要的android项目布局的一部分,用于Android中的Preference Activity.

<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2006 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
Layout for a Preference in a PreferenceActivity. The
Preference is able to place a specific widget for its particular
type in the "widget_frame" layout.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingEnd="?android:attr/scrollbarSize" >

    <ImageView
        android:id="@+android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dip"
        android:layout_marginEnd="6dip"
        android:layout_marginStart="15dip"
        android:layout_marginTop="6dip"
        android:layout_weight="1" >

        <TextView
            android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@android:id/title"
            android:layout_below="@android:id/title"
            android:maxLines="4"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary" />
    </RelativeLayout>
    <!-- Preference should place its actual preference widget here. -->

    <LinearLayout
        android:id="@+android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

(抱歉我的英语不好!!)

xso*_*soh 7

不幸的是,Android 4.2(API级别17)或更高版本仅支持RTL布局支持功能.对于之前的版本,您可以为RTL布局添加布局文件夹.例如,在layout文件夹中放置LTR版本文件布局,在layout-ldrtl文件夹中放置RTL版本文件布局.

至于改变什么来制作RTL取决于你想在RTL中做多少.因为视图的某些部分将保留在同一侧(如导航栏),除非您自定义它.您将需要查看和更改应更改的内容以使其更酷的视图.例如,在LTR视图中,我们可以说:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="left" 
    android:orientation="horizontal"
    android:paddingLeft="8dp"
    android:paddingRight="4dp">
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

然后在RTL视图中你会像这样:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="left" 
    android:orientation="horizontal"
    android:paddingLeft="4dp"
    android:paddingRight="8dp">
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

正如你所看到的那样,我们已经改变了填充中的填充android:paddingLeft="4dp",android:paddingRight="8dp"但是我们保留了android:layout_gravity="left"它,因为它应该像这种情况一样(可能在另一个视图中android:layout_gravity="right").

作为现实生活中的示例,您可以比较Android Quran应用程序RTL audio_panelLTR audio_panel的两个视图.