Android RTL布局方向对齐中心问题

时间:2015-04-27 06:14:09

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

我正在开发一款需要支持阿拉伯语的Android应用程序。 (应该从右到左阅读)。快速搜索解决方案之后,我发现android完全支持阿拉伯语言本身在API级别17中声明

  

机器人:supportsRtl = “真”

在AndroidManifest里面的应用程序标签中,这样我就可以使用布局镜像自动翻转布局,从而获得更好的从右到左的阅读体验。但是,我注意到在布局镜像期间在子RelativeLayout内部的视图中使用centerInParent时会出现问题。以下是我的代码和预期的布局。

<RelativeLayout
    android:background="@color/white"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:padding="20dp">

    <RelativeLayout
        android:background="@drawable/shape_flag_imageview_boarder"
        android:id="@+id/imageLayout"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">

        <ImageView
            android:id="@+id/image"
            android:layout_height="100dp"
            android:layout_width="100dp"
            android:visibility="invisible" />

        <ProgressBar
            android:id="@+id/progressbar"
            android:layout_centerInParent="true"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />

    </RelativeLayout>

    <TextView
        android:id="@+id/text"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_toEndOf="@id/imageLayout"
        android:layout_width="wrap_content"
        android:text="Some text here bla bla bla"
        android:textColor="@color/black" />

</RelativeLayout>

Normal Left to Right Layout

上图显示了从左到右的正常布局方向的预期结果。我在一个子视图中将ImageView和ProgressBar包装在一起的目的是因为我希望在从Internet加载图像时,在ImageView中间显示ProgressBar。在我将Locale更改为阿拉伯语后,它变得像 Layout out in RTL
正如我尝试和错误并弄清楚这是由ProgressBar的centerInParent引起的。它不是以子视图为中心,而是将中心与根外父视图对齐,后者是最外层的RelativeLayout。下面是从ProgressBar中删除centerInParent代码的屏幕截图。

enter image description here

它清楚地显示布局镜像效果很好,但ProgressBar位置不是我所期望的。所以我尝试使用centerVertical和centerHorizo​​ntal,结果分别显示在下面的图像中。 enter image description here enter image description here

这些解决方案都不起作用,我搜索过的主题都与此问题无关。所以我猜这可能是Android库中的一个错误?如果有人知道问题或解决方案,请与我分享。谢谢

3 个答案:

答案 0 :(得分:7)

我通过将android:layoutDirection="ltr"添加到子RelativeLayout来修复它。基本上,它会停用此特定RelativeLayout的RTL格式,android:layout_centerInParent="true"再次正常运行。它解决了我们的特殊问题,因为我们的特定RelativeLayout只包含居中元素。但是,如果布局包含必须正确支持RTL的其他元素(例如文本视图),则不应使用此技巧。希望它有所帮助。

答案 1 :(得分:2)

这是Android框架中的RTL布局错误,仅影响Android 4.2(API 17),并且在AndroidManifest.xml中启用了android:supportsRtl =“true”。

当您使用包含 android:layout_centerVertical="true" android:layout_centerInParent="true" 的项目的 RelativeLayout 时,会发生这种情况。

您可以使用以下Java代码修复它:

View relativeLayout = layoutInflater.inflate(R.layout.my_relative_layout, parent, false);

// Fix RTL layout bug on Android 4.2 (for Arabic and Hebrew mode)
if (Build.VERSION.SDK_INT == 17 &&
       getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
     // Force a left-to-right layout
     relativeLayout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
 }

答案 2 :(得分:0)

让我告诉你正确的答案,看看你的RelativeLayout(id:imageLayout),它的宽度是wrap_content,你的ProgressBar(id:progressbar)添加一个属性android:layout_centerInParent =“true”。这意味着父母不限制witdh,和孩子也想要居中,所以父母会被拉长。