使用RTL语言的ListView布局不一致渲染(阿拉伯语)

时间:2018-04-06 15:24:38

标签: android listview right-to-left

我有一个简单的ListView与ArrayAdapter一起工作很好。问题始于 RTL语言(在本例中为阿拉伯语)。

当你第一次打开它时,一切看起来都不错:

enter image description here

但是在向下滚动并向上滚动后,某些项目似乎呈现错误:

enter image description here

代码很简单。请注意,如果我不重复查看,而是每次无条件地对其进行充气(请参阅下面的注释行),问题是GONE。但这显然是不对的,我想重新使用项目以获得更好的性能和流畅的滚动。

另请注意,我在应用中有多个ListView,并且所有这些都在阿拉伯语中显示了这种不正确的呈现方式。对于这个问题,当我为每个列表项设置文本和图标时,我选择了最简单的情况。

请指教。我执行ArrayAdapter的代码部分:

    public class PagesListAdapter extends ArrayAdapter<IPageRef> {

        ...

        public View getView(int position, View convertView, @NonNull ViewGroup parent) {

            final IPageRef b = getItem(position);
            View row = convertView == null ? mInflater.inflate(R.layout.lang_item, parent, false) : convertView;
            //View row = mInflater.inflate(R.layout.wordlist_item, parent, false);

            TextView title = row.findViewById(R.id.title);
            ImageView img = row.findViewById(R.id.img);

            ... // (setting the title & img properties)

            return row;
        }
    }

lang_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/img"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:layout_width="50dp"
        android:layout_height="38dp"
        android:padding="1dp"
        />

    <TextView
        android:id="@+id/title"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:textSize="25sp" >
    </TextView>

</LinearLayout>

6 个答案:

答案 0 :(得分:1)

将您的布局更改为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:paddingBottom="5dp"
    android:paddingTop="5dp">

    <ImageView
        android:id="@+id/img"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:layout_width="50dp"
        android:layout_height="38dp"
        android:padding="1dp"
        />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/img"
        android:textSize="25sp" />

</RelativeLayout>

答案 1 :(得分:1)

要支持RTL对齐,首先需要将android:supportsRtl="true"添加到清单文件中的<application>元素。

主要内容: -

  • 如果您的应用仅支持API≥17,请将所有layout_marginLeft / layout_marginRight / paddingLeft / paddingRight或任何其他Left和Right布局属性替换为Start和End等效项。例如android:paddingLeft将替换为android:paddingStart。
  • 如果您的应用支持API&lt; 17,则不要替换Left和Right布局属性,而是添加等效的Start和End布局属性。

或者您可以尽可能使用Android Studio > Refactor > Add RTL支持对所有布局执行...

有关RTL的更多参考资料,请参阅本文以了解drawables以及here 它肯定会使用每一步解决您的问题。

答案 2 :(得分:1)

尝试通过在ListAdapter的getView中添加 case 'priceasc': $query->set( 'orderby', 'price' ); $query->set( 'order', 'ASC' ); break; case 'pricedesc': $query->set( 'orderby', 'price' ); $query->set( 'order', 'DESC' ); break; 来基于配置手动设置方向(根据设备语言已相当可靠地对其进行了更新)。

来自:ListView's first entry always incorrect for RTL

这解决了与您类似的问题。

答案 3 :(得分:-1)

尝试对行项使用相对布局,重力为“开始”和“结束”(而不是“左”和“右”)以查看是否有帮助。

(如果你总是希望图像在特定的一面,那么使用左/右而不是开始/结束)

另外,请确保您的Manifest文件中有rtl支持 android:supportsRtl=true

https://developer.android.com/guide/topics/manifest/application-element.html

答案 4 :(得分:-1)

尝试将TextView引力更改为:android:gravity =&#34; center_vertical | right&#34;

答案 5 :(得分:-2)

如果你想使用相同的布局来概括rtl和ltr,我认为最好的方法是处理两个ImageView并根据你的需要制作一个Visible / Gone。像这样:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/img_rtl"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="1dp" />

        <ImageView
            android:id="@+id/img_ltr"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:layout_width="wrap_content"
            android:visibility="gone"
            android:layout_height="wrap_content"
            android:padding="1dp" />

        <TextView
            android:id="@+id/title"
            android:layout_toLeftOf="@id/img_rtl"
            android:layout_toRightOf="@id/img_ltr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:textSize="25sp" >
        </TextView>

</RelativeLayout>

让img_rtl&#39;可见&#39;和img_ltr&#39;去了#39;在阿拉伯语文本的情况下,如果是英语,则从您的getView方法开始。

相关问题