[更正]《Android编程权威指南》9.4定制列表项P167页一个小错误

本文纠正了《Android编程权威指南》9.4章节中关于定制列表项的一个小错误,指出将android:layout_height="match_parent"更改为android:layout_height="wrap_content"的必要性,以避免内容占用整个屏幕。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

*****阅读此篇文章大约需[三分钟],了解知识点[一个]*****

[更正]《Android编程权威指南》9.4定制列表项P167页的小错误:

P167

P167

应将代码 android:layout_height="match_parent"改成android:layout_height=“wrap_content”
不然会导致其内仅仅三个子view的内容却占了整个屏幕

//list_item_crime.xml
    <?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">

    <CheckBox
        android:id="@+id/list_item_crime_solved_check_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:padding="4dp"
        android:text="CheckBox" />

    <TextView
        android:id="@+id/list_item_crime_title_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/list_item_crime_solved_check_box"
        android:padding="4dp"
        android:textStyle="bold"
        android:text="CrimeTitle" />

    <TextView
        android:id="@+id/list_item_crime_date_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/list_item_crime_solved_check_box"
        android:layout_below="@id/list_item_crime_title_text_view"
        android:padding="4dp"
        android:text="CrimeDate" />
</RelativeLayout>

Change to⬇️

<?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">

    <CheckBox
        android:id="@+id/list_item_crime_solved_check_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:padding="4dp"
        android:text="CheckBox" />

    <TextView
        android:id="@+id/list_item_crime_title_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/list_item_crime_solved_check_box"
        android:padding="4dp"
        android:text="CrimeTitle"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/list_item_crime_date_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/list_item_crime_title_text_view"
        android:layout_toLeftOf="@+id/list_item_crime_solved_check_box"
        android:padding="4dp"
        android:text="CrimeDate" />
</RelativeLayout>

按照书上来的话的效果⬇️

一屏只显示一个ViewHolder

改进后⬇️

正常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值