ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-06-12 03:21:53  阅读:247  来源: 互联网

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


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

android:supportsRtl=”true”

在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>

上图显示了从左到右的正常布局方向的预期结果.我在一个子视图中将ImageView和ProgressBar包装在一起的目的是因为我希望在从Internet加载图像时,在ImageView中间显示ProgressBar.在我将Locale更改为阿拉伯语后,它变得像

正如我尝试和错误并弄清楚这是由ProgressBar的centerInParent引起的.它不是以子视图为中心,而是将中心与根外父视图对齐,后者是最外层的RelativeLayout.下面是从ProgressBar中删除centerInParent代码的屏幕截图.

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

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

解决方法:

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

标签:android,android-layout,alignment,arabic,right-to-left
来源: https://codeday.me/bug/20190612/1222888.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有