【问题标题】:How to RTL this android layout?如何 RTL 这个 android 布局?
【发布时间】:2014-12-04 05:38:15
【问题描述】:

我想 rtl(从右到左)在 android 中的布局下方,就像在父布局中使用 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>

(对不起我的英语不好!!)

【问题讨论】:

  • 您是否尝试简单地将android:layoutDirection="rtl" 添加到顶级&lt;LinearLayout&gt; 标记?
  • 我之前加了这个,一切都很好,但它只适用于android 4.2及更高版本,我的min sdk版本是8

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


【解决方案1】:

很遗憾,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 属性(如重力等)进行布局。
  • 当时的问题还不清楚。我已经编辑了答案:D
【解决方案2】:

在根布局中添加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" 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-23
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 2014-03-09
    • 2014-01-05
    相关资源
    最近更新 更多