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

时间:2015-05-14 13:39:16

标签: android height expandablelistview

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

我的ExpandableListView活动的结果视图:

enter image description here

你怎么看,有些行没有正确的高度,有些行被裁剪。

我做错了什么?

有我的布局文件

活动布局:

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

2 个答案:

答案 0 :(得分:2)

谢谢大家的回答。

解决方案是使用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>

答案 1 :(得分:0)

尝试将layout_height的{​​{1}}设置为TextView noticeDate

wrap_content