我正在开发一个需要支持阿拉伯语的 Android 应用程序。 (应该从右到左阅读)。在快速搜索解决方案后,我发现 android 在 API 级别 17 中完全支持阿拉伯语,并声明了
android:supportsRtl="true"
在 AndroidManifest 内的应用程序标签中,以便我可以使用布局镜像自动翻转布局以获得更好的从右到左阅读体验。但是,我注意到在布局镜像期间在子 RelativeLayout 内部的 View 中使用 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>
上图显示了从左到右的正常布局方向的预期结果。我将 ImageView 和 ProgressBar 包装在一个 subview 中的目的是因为我希望在从 Internet 加载图像时 ProgressBar 显示在 ImageView 的中间。在我将 Locale 更改为 Arabic 之后,它变成了
正如我尝试错误并弄清楚这是由 ProgressBar 的 centerInParent 引起的。它不是在 subview 内居中,而是将中心对齐到最外层 RelativeLayout 的根父 View 。下面是从 ProgressBar 中删除 centerInParent 代码的屏幕截图。
它清楚地表明布局镜像工作正常,但 ProgressBar 位置不是我所期望的。所以我尝试在 centerVertical 和 centerHorizontal 上工作,结果分别如下图所示。
所有解决方案均无效,而且我搜索的主题均与此问题无关。所以我想这可能是 Android 库中的错误?如果有人知道问题或解决方案,请与我分享。谢谢
最佳答案
我通过将 android:layoutDirection="ltr"
添加到子 RelativeLayout 来修复它。基本上,它停用了此特定 RelativeLayout 的 RTL 格式,并且 android:layout_centerInParent="true"
再次正常运行。它解决了我们的特定问题,因为我们特定的 RelativeLayout 仅包含居中元素。但是,如果布局包含必须正确支持 RTL 的其他元素(例如 TextView ),则不应使用此技巧。希望对您有所帮助。
关于Android RTL布局方向对齐中心问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29888439/