【问题标题】:Height of views inside ScrollViewScrollView 内视图的高度
【发布时间】:2016-03-12 03:13:37
【问题描述】:

我有一个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 + "");
}

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

【问题讨论】:

  • 更改列表视图高度匹配父项
  • 列表视图是否滚动?意味着有足够的对象列表视图可以滚动或覆盖整个空间?
  • 不,列表视图没有滚动。 ListView + TextView 被放在 ScrollView 中,共同滚动,而不是单独滚动。
  • 是的,这就是问题所在。两个滚动视图没有一起滚动。 ListView 也有滚动视图属性,即滚动。你为什么使用滚动视图?
  • 我有一个列表视图和下面的几个文本视图。所有这些都无法包含在屏幕中。所以,我把它们都放在了一个 ScrollView 中。

标签: android listview scrollview


【解决方案1】:
    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>

【讨论】:

  • 感谢您的回复,但我们可以在RelativeLayout 中使用orientation 吗?
  • 哦,不,那行写错了。对于那个很抱歉。如果您删除该行代码也将顺利运行。
  • 对不起,它不起作用。 listView 未显示完整。它是可滚动的,但它并没有完全显示出来。我正在尝试制作屏幕,以便完全显示列表视图,并使其下方的 TextViews 跟随。因此,当我滚动时,我从 listView 的第一项开始,并在 textview 的最后一个文本处停止。这只是修复了底部的文本视图,更大的列表视图在上面被吃掉了。
  • 好的,但您正在尝试不可能的滚动技术。我提供了最好的解决方案。让事情变得简单而通用。此代码将在每个屏幕尺寸上顺利运行。祝您的代码好运。
  • 解决方案很好,但它根本无法回答所提出的问题。问题很直接。如何在 Android 屏幕上显示一个大的 ListView 后跟一个 textview?
【解决方案2】:

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

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

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

这样一来,所有内容都可以一次性滚动,并且所有布局都显示为没有任何剪切。好消息是ListView 可以有任意数量的页眉和页脚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多