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

Android RTL布局方向对齐中心问题,android,android-layout,alignment,arabic,right-to-left,Android,Android Layout,Alignment,Arabic,Right To Left,我正在开发一个Android应用程序,需要支持阿拉伯语。(应从右向左读取)。在快速搜索解决方案后,我发现android完全支持API级别17的阿拉伯语,并声明 android:supportsRtl=“true” 在AndroidManifest内部的应用程序标记中,这样我就可以使用布局镜像来自动翻转布局,以获得更好的从右到左的阅读体验。但是,我注意到在布局镜像期间在子相对论视图内部使用centerInParent时出现了一个问题。下面是我的代码和预期布局 <RelativeLayout

我正在开发一个Android应用程序,需要支持阿拉伯语。(应从右向左读取)。在快速搜索解决方案后,我发现android完全支持API级别17的阿拉伯语,并声明

android:supportsRtl=“true”

在AndroidManifest内部的应用程序标记中,这样我就可以使用布局镜像来自动翻转布局,以获得更好的从右到左的阅读体验。但是,我注意到在布局镜像期间在子相对论视图内部使用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>

上图显示了从左到右的正常布局方向的预期结果。我在IsVIEW中包装IVIEVIEW和PrimeStar的目的是因为当图像从Internet加载时,我希望在IVIEVIEW中间显示PraveStar栏。在我将语言环境更改为阿拉伯语后,它变得像
正如我尝试和错误,并找出这是由ProgressBar的centerInParent造成的。它不是在子视图内居中,而是将中心与根父视图对齐,根父视图是最外部的相对视图。下面是从ProgressBar中删除centerInParent代码的屏幕截图

它清楚地表明布局镜像工作良好,但ProgressBar的位置不是我所期望的。因此,我尝试使用centerVertical和centerHorizontal,结果分别如下图所示。


没有一个解决方案是有效的,我搜索的主题也没有一个与这个问题相关。所以我猜这可能是安卓库的一个bug?如果有人知道问题或解决方案,请与我分享。谢谢

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

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

当您使用包含使用android:layout\u centerVertical=“true”或android:layout\u 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);
 }

让我告诉你正确的答案,看看你的RelativeLayout(id:imageLayout),它的宽度是wrap\u content,你的ProgressBar(id:ProgressBar)添加了一个属性android:layout\u centerInParent=“true”。这意味着父母不限制witdh,孩子也希望居中,因此父级将被拉伸。

至少部分问题是您没有指定任何内容应该完全展开的位置-外部相对布局中的任何内容都没有与父级中的任何内容对齐。因此,我并不感到惊讶,因为它不知道如何正确地布局它。您要定位progressbar的父RelativeLayout没有固定的高度和宽度。因此,这可能会导致不期望的结果。@Gabeschen穿着短裤,上面显示的从左到右的布局是我所期望的,我期望它看起来完全一样,但在阿拉伯语区域设置中变为从右到左。我没有使用任何左对齐或右对齐,而是使用“开始”和“结束”,所以我希望它可以很好地工作,但事实并非如此。@TheLittleNaruto我尝试在相对位置设置一个固定的高度和宽度,结果出人意料地工作。但我希望它是包装内容,而不是放置固定的高度和宽度,因为ImageView的大小可能不同,这是在运行时确定的。另外,为什么我把progressbar放在LTR中,而不是RTL中的相对论呢?我不知道为什么它只在
RTL
的情况下不起作用。但它不应该在
LTR
的情况下工作。因为这就是
RelativeLayout
的工作原理。我们将每个视图相对于其他视图放置在
RelativeLayout
中,在这种情况下,它应该具有固定的高度和宽度。