RelativeLayout内部的两个TextViews对rtl语言环境有适当的响应

时间:2019-07-10 15:01:29

标签: android relativelayout

如何在TextViewsRelativeLayout语言环境中在LTR中创建2个RTL

(表仅用于清除我的意思,这不是我的问题的一部分)

LTR

╔════════════╦═══════════════════════════════════════════════════════════╗
║  txtView1  ║                        txtView2                           ║
╚════════════╩═══════════════════════════════════════════════════════════╝

RTL

╔═════════════════════════════════════════════════════════════╦══════════╗
║                           txtView2                          ║ txtView1 ║
╚═════════════════════════════════════════════════════════════╩══════════╝

当前我使用的是边距,但这似乎不正确:

<RelativeLayout
    android:id="@+id/title_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/txtView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView 1"
        android:textColor="?attr/info_color"
        android:textSize="13sp" />

    <TextView
        android:id="@+id/txtView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/txtView1"
        android:text="TextView 2"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp" />

</RelativeLayout>

我不能使用setLayoutDirectionminSdkVersion在17“以下

2 个答案:

答案 0 :(得分:0)

我不确定这是否是您想要的,但是您可以:

1-在android:layout_width中将wrap_content设置为TextView1

2-使TextView1与父母的起点对齐

3-将TextView2的末尾的TextView1对齐

<RelativeLayout
    android:id="@+id/title_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/txtView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:text="TextView 1"
        android:textSize="13sp" />

    <TextView
        android:id="@+id/txtView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_alignBaseline="@id/txtView1"
        android:layout_toEndOf="@id/txtView1"
        android:text="TextView 2"/>
</RelativeLayout>

这是结果:

LTR

enter image description here

RTL

enter image description here

编辑

正如评论中所提到的,您可以使用LinearLayout

<LinearLayout
    android:id="@+id/title_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1">

    <TextView
        android:id="@+id/txtView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="TextView 1"
        android:textSize="13sp"
        android:layout_weight="0.2"/>

    <TextView
        android:id="@+id/txtView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="0.8"
        android:layout_alignBaseline="@id/txtView1"
        android:text="TextView 2"/>
</LinearLayout>

答案 1 :(得分:0)

以下代码对我来说似乎正常工作。

<LinearLayout
        android:layoutDirection="locale"
        android:textDirection="locale" //Works as expected when I set rtl or ltr
        android:layout_width="match_parent" 
        android:layout_height="match_parent">
    <TextView
            android:text="Something"
            android:layout_weight="2"
            android:textDirection="inherit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>
    <TextView
            android:text="or the other"
            android:layout_weight="8"
            android:textDirection="inherit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>
</LinearLayout>