出于某种原因,当我使用 RTL 时,我的 ListView 中的第一个条目总是以错误的方式翻转。例如:
附加信息:
<ImageView
android:id="@+id/dropdown_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/photo_button_disabled"
android:layout_gravity="center_horizontal"/>
<ImageView
android:id="@+id/count_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:scaleType="fitCenter"
android:layout_alignEnd="@+id/dropdown_icon"
android:layout_alignRight="@+id/dropdown_icon"
android:layout_alignStart="@+id/dropdown_icon"
android:layout_alignLeft="@+id/dropdown_icon"
android:layout_alignTop="@+id/dropdown_icon"
android:layout_alignBottom="@+id/dropdown_icon"
android:adjustViewBounds="true"/>
虽然对话框 fragment 的布局看起来像:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:background="@drawable/rounded_bg">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textStyle="bold"
android:textSize="@dimen/dialog_title"
android:padding="10dp"/>
<LinearLayout
android:layout_below="@+id/title"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- This table should be populated with all the different options -->
<ListView
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/entry_list"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle"
android:background="@color/overlay"
android:descendantFocusability="afterDescendants"
android:animateLayoutChanges="true">
</ListView>
<!-- The save and clear buttons-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="@dimen/general_padding"
android:paddingTop="@dimen/general_padding"
android:paddingRight="@dimen/general_padding">
<TextView
android:id="@+id/warning_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/recon_value_required"
android:visibility="gone"
android:textColor="@color/warningColour"
android:layout_marginBottom="@dimen/list_vertical_margin"/>
<LinearLayout
android:id="@+id/button_container"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
android:layout_below="@id/warning_label"
android:paddingBottom="@dimen/general_padding">
<Button
android:id="@+id/clear_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/rounded_button"
android:text="@string/clear"
android:textColor="@color/buttonTextColour"
android:layout_weight="1"
android:foreground="?attr/selectableItemBackground"
android:layout_marginRight="8dp"
android:layout_marginEnd="8dp"/>
<Button
android:id="@+id/save_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/rounded_button"
android:text="@string/save"
android:textColor="@color/buttonTextColour"
android:layout_weight="1"
android:foreground="?attr/selectableItemBackground"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
我不确定还能尝试什么。我已经摆弄了不同的布局。改变方向并不能解决问题。任何帮助将不胜感激。
最佳答案
在探索中,我发现问题只发生在对话框中的 ListViews 上(我试过 AlertDialog),并且在向下和向上滚动后,最上面的 ListView 行的方向性是固定的。我不确定为什么会这样,只能建议
rowView.setLayoutDirection(getContext().getResources().getConfiguration().getLayoutDirection());
根据配置手动设置方向(根据设备语言非常可靠地更新)在 getView
您的 ListAdapter 附言见 this question有同样的问题
附言getView 似乎只对对话框中的 ListViews 而不是 Fragments/Activity 中的 ListViews 被调用很多且不规则,并且可能值得研究。虽然不能保证 ListView 的行为,但还是有点奇怪
关于android - ListView 的第一个条目对于 RTL 总是不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55843642/