我正在开发一个Android应用程序,需要支持阿拉伯语言(应该从右到左阅读)。在快速搜索了解解决方案后,我发现android从API 17版本开始原生支持阿拉伯语言,并且在AndroidManifest文件中的应用标签中声明
android:supportsRtl="true"
以便使用布局镜像自动翻转布局,获得更好的从右到左阅读体验。但是,在布局镜像中使用centerInParent设置子RelativeLayout视图时出现问题。以下是我的代码和预期的布局。
<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一起包装在一个子视图中的目的是,当从互联网加载图像时,我希望ProgressBar显示在ImageView的中间位置。但是,当我将Locale更改为阿拉伯语后,它变成了如下所示:
经过尝试和错误,我发现这是因为进度条的centerInParent属性导致的。它不是居中对齐在子视图内部,而是在最外层RelativeLayout的根父视图中心对齐。下面是移除了进度条的centerInParent代码的截图。
这清楚地显示了布局镜像效果良好,但ProgressBar的位置不是我期望的。因此,我尝试处理centerVertical和centerHorizontal属性,分别显示如下所示的结果图片。
没有一个解决方案能够解决问题,我搜索的主题也没有相关的信息。因此,我猜测这可能是Android库中的一个错误?如果有人知道解决方案或问题,请与我分享。谢谢。
android:gravity="center"
吗? - zdd