Listview与内容相同的高度

Nic*_*ick 16 android android-layout android-listview

我使用自定义列表视图.内容来自动态我想要与内容相同的listview高度.

我使用了wrap_content,但这没有帮助.如果我删除scrollview然后它的工作

码."垂直滚动ScrollView不应包含另一个垂直滚动小部件(ListView)"

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_img"
    android:scrollbars="none" >

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

<ListView
            android:id="@+id/lstsemtrack"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        android:divider="#f2e4e4"
        android:dividerHeight="1dip"
             >
        </ListView>
Run Code Online (Sandbox Code Playgroud)

物品清单

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dip"
    android:scrollbars="none" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="4" >

        <TextView
            android:id="@+id/txtBiology"
            style="@style/sem_rowtext"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="5dip"
            android:layout_weight="1"
            android:text="Biology" />

        <TextView
            android:id="@+id/txtClass"
            style="@style/sem_rowtext"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Biology - 101" />

        <TextView
            android:id="@+id/txtGrade"
            style="@style/sem_rowtext"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Grade" />

        <TextView
            android:id="@+id/txtGrade"
            style="@style/sem_rowtext"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Remove" />
    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

输出如下我想要内容没有scrollview

在此输入图像描述

Raj*_*008 35

使用此代码

public class MyListView  extends ListView {

    public MyListView  (Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyListView  (Context context) {
        super(context);
    }

    public MyListView  (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 12

您不应该将ListView放在ScrollView中,因为ListView本身是一个显示可滚动项列表的视图组.如果在scrollview中使用它,它将不会接收滚动事件,因为它们都由父ScrollView处理.使用ListView使其不滚动是非常昂贵的,并违背ListView的整个目的.你不应该这样做.只需使用LinearLayout即可.

但是,如果你真的想这样做,你可以看一下:如何将ListView放入ScrollView而不会崩溃?


Vik*_*amV 8

在父布局中将高度设置为wrap_content.这应该工作.


Pra*_*asa -1

使用 android:layout_height="match_parent",我认为它会起作用。