自定义 ListView 项目视图中的 TextView 未填充 API 中的 parent 18 & 19
TextView in custom ListView item view isn't filling parent in API 18 & 19
我有一个使用自定义列表项视图的自定义列表视图适配器。自定义列表项视图在左侧有一个图像视图,带有两个 TextView 的堆栈,顶部是主标题,右侧是副标题。我想要 发生的是让副标题消失,如果没有要显示的副文本,则主要文本填充space。
我已经针对 16 到 23 岁之间的所有其他 phone / 平板电脑 API 进行了测试。一切都按预期工作,除了我 运行 在设备 运行 Android 4.3.1 (API 18) Android 4.4.2 (API 19)。 运行 针对 API 18 或 19,字幕视图的可见性确实设置为 View.GONE,但主标题视图不会调整大小以占据 space.
有人知道这里发生了什么吗?
这是列表项的布局文件:
list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:id="@+id/list_item_snapshot_imageview"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@color/medium_grey"
android:contentDescription="@string/item_snapshot" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@id/list_item_snapshot_imageview"
android:layout_alignTop="@id/list_item_snapshot_imageview"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_toEndOf="@id/list_item_snapshot_imageview"
android:layout_toRightOf="@id/list_item_snapshot_imageview"
android:orientation="vertical">
<TextView
android:id="@+id/list_item_title_textview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20sp"
tools:text="Main Text" />
<TextView
android:id="@+id/list_item_subtitle_textview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="14sp"
tools:text="Subtext" />
</LinearLayout>
</RelativeLayout>
这是负责更新内容的 java 代码。 viewHolder
只是一个静态 class 包含对 TextView 的引用,并保存在列表项的标签中以供视图回收。
NamedItem item = getRowItem(section, position);
if (item != null) {
viewHolder.titleTextView.setText(item.getName());
if (item.getSubtitle() != null) {
String subtitle = ((ItemInfo) item).getSubtitle();
viewHolder.subtitleTextView.setText(subtitle);
viewHolder.subtitleTextView.setVisibility(View.VISIBLE);
} else {
viewHolder.subtitleTextView.setVisibility(View.GONE);
}
}
编辑
我做了更多测试,API 18 和 19 也发生了这种情况。我更改了其余描述以反映这一点。
想通了。 API 18 和 19 中处理 ListView 项目视图的方式看起来有些不喜欢将 RelativeLayout 作为自定义 ListView 项目中的根布局。将整个布局包裹在 LinearLayout 中解决了问题:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Other Views ... -->
</RelativeLayout>
</LinearLayout>
在遇到这个 SO 答案后尝试了这个:
仍然无法解释为什么会发生这种情况。
我有一个使用自定义列表项视图的自定义列表视图适配器。自定义列表项视图在左侧有一个图像视图,带有两个 TextView 的堆栈,顶部是主标题,右侧是副标题。我想要 发生的是让副标题消失,如果没有要显示的副文本,则主要文本填充space。
我已经针对 16 到 23 岁之间的所有其他 phone / 平板电脑 API 进行了测试。一切都按预期工作,除了我 运行 在设备 运行 Android 4.3.1 (API 18) Android 4.4.2 (API 19)。 运行 针对 API 18 或 19,字幕视图的可见性确实设置为 View.GONE,但主标题视图不会调整大小以占据 space.
有人知道这里发生了什么吗?
这是列表项的布局文件:
list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:id="@+id/list_item_snapshot_imageview"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@color/medium_grey"
android:contentDescription="@string/item_snapshot" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@id/list_item_snapshot_imageview"
android:layout_alignTop="@id/list_item_snapshot_imageview"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_toEndOf="@id/list_item_snapshot_imageview"
android:layout_toRightOf="@id/list_item_snapshot_imageview"
android:orientation="vertical">
<TextView
android:id="@+id/list_item_title_textview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20sp"
tools:text="Main Text" />
<TextView
android:id="@+id/list_item_subtitle_textview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="14sp"
tools:text="Subtext" />
</LinearLayout>
</RelativeLayout>
这是负责更新内容的 java 代码。 viewHolder
只是一个静态 class 包含对 TextView 的引用,并保存在列表项的标签中以供视图回收。
NamedItem item = getRowItem(section, position);
if (item != null) {
viewHolder.titleTextView.setText(item.getName());
if (item.getSubtitle() != null) {
String subtitle = ((ItemInfo) item).getSubtitle();
viewHolder.subtitleTextView.setText(subtitle);
viewHolder.subtitleTextView.setVisibility(View.VISIBLE);
} else {
viewHolder.subtitleTextView.setVisibility(View.GONE);
}
}
编辑 我做了更多测试,API 18 和 19 也发生了这种情况。我更改了其余描述以反映这一点。
想通了。 API 18 和 19 中处理 ListView 项目视图的方式看起来有些不喜欢将 RelativeLayout 作为自定义 ListView 项目中的根布局。将整个布局包裹在 LinearLayout 中解决了问题:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Other Views ... -->
</RelativeLayout>
</LinearLayout>
在遇到这个 SO 答案后尝试了这个:
仍然无法解释为什么会发生这种情况。