【问题标题】:How to automatically set different item height in ExpandableListView?如何在 ExpandableListView 中自动设置不同的项目高度?
【发布时间】:2015-05-14 23:01:50
【问题描述】:

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

ExpandableListView 有我的活动的结果视图:

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

我做错了什么?

有我的布局文件

活动布局:

<?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 中最高为 20 dp 使其成为 wrap_content
  • android:lines=2设置为noticeTitleTextView
  • 1 - 您可以使用单个 RelativeLayout 而不是 2 个 LinearLayout(更扁平的设计 = 更好的性能)。 2 - fill_parent 已弃用。

标签: android height expandablelistview


【解决方案1】:

感谢大家的回答。

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

【讨论】:

    【解决方案2】:

    尝试将layout_height for TextView noticeDate 设置为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>
    

    【讨论】:

      猜你喜欢
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 2014-09-24
      • 2021-02-19
      • 1970-01-01
      相关资源
      最近更新 更多