关于listview在ScrollView中不能全屏的问题

来源:互联网 发布:个人可以注册域名吗 编辑:程序博客网 时间:2024/06/08 07:43

1,

 Utility.setListViewHeightBasedOnChildren(lvHistory);

public static class Utility {

        public static void setListViewHeightBasedOnChildren(ListView listView) {
            ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null) {
                // pre-condition
                return;
            }

            int totalHeight = 0;
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }

            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight
                    + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            listView.setLayoutParams(params);
        }

    }该方法在设置adapter后使用


2,推荐使用,在ScrollView中设置 android:fillViewport="true"该属性

  <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:scrollbars="none" >

</ScrollView>即可

0 0