Android ListView不会扩展整个屏幕?

时间:2011-09-06 14:34:01

标签: android listview

我有以下活动:

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/FiltersLayout" android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    >
    <ScrollView android:id="@+id/FiltersScroll"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <ExpandableListView android:id="@+id/featureList"
            android:layout_width="fill_parent" android:stretchColumns="*" android:orientation="vertical" android:layout_height="fill_parent">

        </ExpandableListView>

    </ScrollView>
</LinearLayout>

我需要ExpandableListView来划伤屏幕,但它显示如下:

![Anzroiz ListView] [1]

2 个答案:

答案 0 :(得分:3)

U可以在将适配器设置到列表后调用此方法....这个工作

private  void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }
        int totalHeight = 0;
        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }

答案 1 :(得分:1)

尝试将ScrollView的高度更改为fill_parent,但实际上您应该完全省略ScrollView,因为ListView提供了自己的滚动。