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

Wai*_*ong 4 android alignment arabic android-layout 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>
Run Code Online (Sandbox Code Playgroud)

从左到右的正常布局

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

在此输入图像描述

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

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

小智 7

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