ScrollView 内视图的高度

Height of views inside ScrollView

我有一个 ScrollView,里面有一个 ListView,后跟几个 TextView。我所拥有的 ListView 是使用自定义适配器填充的,项目的宽度可能会因我们所在的页面而异。这是我的 ScrollView:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/options_listview">
        </ListView>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/options_listview">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="24dp"
                android:text="Explanation:"
                android:id="@+id/explanation_header"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/explanation_text"
                android:layout_below="@+id/explanation_header"
                android:text="stuff..."/>
        </RelativeLayout>
    </RelativeLayout>
</ScrollView>

问题

ListView 的高度未完全显示。我以编程方式将高度设置为:

public static void setListViewHeight(ListView list) {

    int height = 0;

    for (int i = 0; i < list.getCount(); i++) {
        View childView = list.getAdapter().getView(i, null, list);
        childView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        height+= childView.getMeasuredHeight();
    }

    //dividers height
    height += list.getDividerHeight() * list.getCount();


    ViewGroup.LayoutParams params = list.getLayoutParams();
    params.height = height;
    list.setLayoutParams(params);

    Log.i("Height ", height + "");
}

现在我在日志中看到,无论项目高度如何,所有 listView 的高度都相同。我怎样才能使 ListView 在我的滚动视图中完全可见?

    Please try this layout:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
                    android:layout_height="match_parent">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/tVL"
            android:id="@+id/options_listview">
        </ListView>

        <RelativeLayout
            android:id="@+id/tVL"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="24dp"
                android:text="Explanation:"
                android:id="@+id/explanation_header"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/explanation_text"
                android:layout_below="@+id/explanation_header"
                android:text="stuff..."/>
        </RelativeLayout>
    </RelativeLayout>

正如 Khizar Hayat 指出的那样,ListView 不应在 ScrollView 中使用,因为列表视图已经具有滚动功能。这样做会产生设计冲突。

所以,制作 3 种不同的布局解决了我的问题:

  1. 在第一个布局文件中保留 ListView
  2. 制作另一个布局,header.xml 并将 ListView 上方的所有内容放入该布局。
  3. 进行第三种布局,footer.xml 并在该页脚布局中将所有内容保持在 ListView 下方。
  4. 在 activity class 中扩充这些页眉和页脚布局,并将页眉和页脚添加到 ListView

这样一来,所有的内容都一次性滚动,所有的布局都显示出来,没有任何剪切。好处是我们可以在 ListView.

中添加任意数量的页眉和页脚