RTL无法用于布局

时间:2018-07-24 09:59:09

标签: android android-linearlayout

我添加了所有必要的内容以支持英语阿拉伯语应用程序的RTL

  

android:supportsRtl =“ true”

到清单文件中的元素。 将所有“左/右”布局属性更改为新的“开始/结束”等效项。 一切工作正常,除了该布局在RTL中仍然为空白,但在LTR中它却像一种魅力:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:background="@color/white" 
 android:layoutDirection="rtl"    
 android:layout_height="wrap_content">
  <LinearLayout
    android:id="@+id/limit_levels"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_weight="1"
    android:layout_margin="5dp"
    android:layout_height="wrap_content" >

      <RelativeLayout
        android:id="@+id/level1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:tint="@color/white" >
        <TextView
            android:id="@+id/level1_icon"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:background="@drawable/rounded_corner"
            android:backgroundTint="@color/soft_grey"
            android:clickable="true"
            android:alpha="0.3"
            android:gravity="center"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="2dp"
            android:layout_marginRight="2dp"
            android:text="1"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold"
            android:visibility="visible" />
        <RelativeLayout
            android:id="@+id/state_item_level1_indicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:layout_marginLeft="15dp"
            android:tint="@color/white" >
            <ImageView
                android:id="@+id/small_circle_level1_indicator"
                android:layout_width="15dp"
                android:layout_height="15dp"
                android:layout_marginTop="3dp"
                android:src="@drawable/oval_small_mail" />

            <TextView
                android:id="@+id/small_circle_icon_level1_indicator"
                android:layout_width="15dp"
                android:layout_height="15dp"
                android:layout_centerInParent="true"
                android:textStyle="bold"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:text="1"
                android:textColor="@color/white"
                android:tint="@color/white"
                android:visibility="visible" />
        </RelativeLayout>
        <TextView
            android:id="@+id/small_circle_level1"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:background="@drawable/rounded_corner"
            android:textSize="11dp"
            android:textStyle="bold"
            android:visibility="gone"
            android:gravity="center"
            android:text="+2"
            android:textColor="@color/TealDark"
            android:layout_alignRight="@id/small_circle_level1"
            android:layout_marginBottom="25dp"
            android:layout_alignParentRight="true"
            android:layout_centerInParent="true"
            android:layout_alignParentTop="true"/>
    </RelativeLayout> 
    <RelativeLayout
        android:id="@+id/state_item_infraction"            
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="3dp"
        android:tint="@color/white"
        android:visibility="visible">
        <TextView
            android:id="@+id/infraction_icon"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_marginEnd="2dp"
            android:layout_marginRight="2dp"
            android:layout_marginTop="8dp"
            android:alpha="0.3"
            android:background="@drawable/rounded_corner"
            android:backgroundTint="@color/soft_grey"
            android:clickable="true"
            android:gravity="center"
            android:text="Inf"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold"
            android:visibility="visible" />
        <RelativeLayout
            android:id="@+id/state_item_inf_indicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            android:layout_marginLeft="18dp"
            android:tint="@color/white" >
            <ImageView
                android:id="@+id/small_circle_inf_indicator"
                android:layout_width="15dp"
                android:layout_height="15dp"
                android:src="@drawable/oval_small_mail" />

            <TextView
                android:id="@+id/small_circle_icon_inf_indicator"
                android:layout_width="15dp"
                android:layout_height="14dp"
                android:layout_centerInParent="true"
                android:textStyle="bold"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:text="12"
                android:textSize="10sp"
                android:textColor="@color/white"
                android:tint="@color/white"
                android:visibility="visible" />
        </RelativeLayout>
    </RelativeLayout>        
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

//rtl
public static void forceRTLIfSupported(Window window, Activity activity) {
    if (activity.getResources().getString(R.string.isRTL).equals("true")) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            window.getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        }
    }
}

call all activity this method

Method.forceRTLIfSupported(getWindow(), AboutUs.this);

Method my class name