如何在 ExpandableListView 中自动设置不同的项目高度?

How to automatically set different item height in ExpandableListView?

我正尝试在我的项目中使用 ExpandableListView 和自定义适配器。

我的 activity 有一个结果视图 ExpandableListView:

您怎么看,有些行的高度不正确,有些行被裁剪了。

我做错了什么?

有我的布局文件

activity布局:

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

    <ExpandableListView
        android:layout_width="matc_parent"
        android:layout_height="match_parent"
        android:id="@+id/noticeList"/>

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/empty"
        android:text="@string/empty"/>

</LinearLayout>

ExpandableListView 项目的布局:

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

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/noticeImage"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="20dp"
            android:id="@+id/noticeDate"/>

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:id="@+id/noticeTitle"/>

    </LinearLayout>

</LinearLayout>

尝试将 TextView noticeDatelayout_height 设置为 wrap_content

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

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/noticeImage"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/noticeDate"/>

            <TextView
                android:layout_height="match_parent"
                android:layout_width="fill_parent"
                android:id="@+id/noticeTitle"/>

        </LinearLayout>

    </LinearLayout>

感谢大家的回答。

解决方案是使用 RelativeLayout 代替 LinearLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/noticeImage"/>



    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/noticeImage"
        android:id="@+id/noticeDate"
        />

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/noticeTitle"
        android:layout_toRightOf="@+id/noticeImage"
        android:layout_below="@+id/noticeDate"
        />    
</RelativeLayout>