android:gravity ="right"不适用于阿拉伯语和其他RTL语言的所有设备

M.E*_*.ES 8 java android arabic android-layout right-to-left

我有一个阿拉伯语Android应用程序,这里是XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/greygradientbackground">
<ImageView android:id="@+id/logo"
    android:layout_width="150dp"
    android:layout_height="fill_parent"
    android:scaleType="centerCrop"
    android:layout_margin="5dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true" />
<TextView android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:textColor="#000000"
    android:layout_gravity="center_vertical|right"
    android:gravity="right"
    android:layout_toLeftOf="@id/logo"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

问题是android:gravity适用于某些Android机型而其他机型则不适用.

更具体地说,我已经在许多2.3.3安卓设备上测试了应用程序,并且阿拉伯语文本对齐了.但是在其他2.3.3设备上,阿拉伯语文本左对齐(这是错误的).

当我改变android:gravity ="right"到android:gravity ="left"时,问题从第二组设备转移到第一组.

所以我的问题是如何解决这个问题,特别是据我所知,没有办法根据设备型号来本地化布局.

提前感谢任何指导,因为我完全迷失了.:(

更新:

我搜索了"如何在所有Android版本上正确对齐阿拉伯语?" 但发现我的所有测试设备都没有工作.有什么建议吗?我确信有一种最佳实践方法可以在Android上对齐阿拉伯语文本.

更新2:

我尝试使用WebView而不是TextView使用CSS样式正确对齐阿拉伯语.但是,阿拉伯语文本在WebView中显示为奇怪的字符.

这是代码:

mWebView.loadData("<html  dir=\"rtl\">?????<body></body></html>", "text/html", "UTF-8");
Run Code Online (Sandbox Code Playgroud)

奇怪的是,阿拉伯语网站文本显示正确.所以有什么问题?:(

Mur*_*ira 5

这是预期的行为。您可能不使用gravity="right",而是使用gravity="end"您所应用的相同想法gravity="left",您可以使用gravity="start",以及layout_marginStart代替layout_marginLeft

这样,Android 会将文本置于“开始”方向,具体取决于位置。(对我们来说,美国人,我们开始从写左边,我们最终右边,但阿拉伯人开始右侧结束左边)。

您可以使用方法获取布局/文本方向getLayoutDirectionFromLocale(null),这将返回View.LAYOUT_DIRECTION_LTR(从左到右)或View.LAYOUT_DIRECTION_RTL(从右到左,如阿拉伯语)。

您可以在此处查看更多信息


sag*_*gar 0

将布局中的 textveiw 更改为......

<TextView android:id="@+id/title"
Run Code Online (Sandbox Code Playgroud)

将您的layout_width更改为“fill_parent”

     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:textSize="16sp"
     android:textColor="#000000"
     android:layout_gravity="center_vertical|right"
     android:gravity="right"
     android:layout_toLeftOf="@id/logo"
/>
Run Code Online (Sandbox Code Playgroud)

您也可以以另一种方式更改该内容...您可以在活动类中进行更改...如下所示...让您的带有 id 标题的 TextView 映射到活动类中,并使用名为“tv”的 TextView 对象可以通过写这个来使其正确对齐。

 tv.setGravity(Gravity.RIGHT);
Run Code Online (Sandbox Code Playgroud)

但在这两种情况下,宽度布局宽度应该是“fill_parent”。如果您不这样做并将其保留为“wrap_content”,那么文本就没有空间移动...所有对齐方式(中心,左,右) ...)保持不变.....

如果这两个都不起作用...那么我希望您使用表格布局并将文本视图保留在其中...