如何对这个Android布局进行RTL(从右到左)处理?

6
我希望你能在Android中担任从右到左(RTL)的翻译工作,就像在父布局中使用layoutDirection ="rtl"时一样(但它仅适用于4.2及更高版本),并找到任何使其清晰地向右到左的方法。这是用于Android中的首选项活动的主要项目布局的一部分。
<?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>

抱歉,我的英语不太好!!

对不起,我的英语不太好!!


1
你尝试过在顶层的<LinearLayout>标签中简单地添加android:layoutDirection="rtl"吗? - Ted Hopp
我在之前添加了这个,一切都很好,但它只适用于Android 4.2及更高版本,而我的最小SDK版本是8。 - Mokhtarabadi
2个回答

8
很遗憾,RTL布局支持功能仅适用于Android 4.2(API级别17)或更高版本。对于之前的版本,您可以为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>

然后在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>

如您所见,我们已更改android:paddingLeft="4dp"android:paddingRight="8dp"中的填充,但保留了android:layout_gravity="left",因为对于这种情况应该是这样的(也许在另一个视图上将是android:layout_gravity="right")。
作为现实生活中的例子,您可以比较Android Quran应用程序RTL audio_panelLTR audio_panel的两个视图。

谢谢,我知道这个,但我的问题是如何使用XML属性(如gravity等)来实现RTL布局。 - Mokhtarabadi
问题到那时还不够清晰。我已经编辑了答案:D - xsoh

0
在根布局中添加android:layoutDirection="rtl"
<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:layoutDirection="rtl"
        android:minHeight="?android:attr/listPreferredItemHeight"
        android:paddingEnd="?android:attr/scrollbarSize" 

我在2014年问过这个问题,但还是谢谢! - Mokhtarabadi

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