【问题标题】:Android - ListView's height just fits 1 ListView itemAndroid - ListView 的高度仅适合 1 个 ListView 项
【发布时间】:2015-09-11 12:16:23
【问题描述】:

我正在使用 Android Studio 并设计如下布局。但是,我的 ListView lv_comics 仅显示 1 个项目的足够空间。我试图将 layout_height 设置为 fill_parent,但它仍然不起作用。但是,当我将 layout_height 设置为特定尺寸时,它是可以的,但是当我切换到另一个屏幕尺寸时,这并不好。谁能帮帮我?

<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/layout_swiperefresh"
android:background="@color/grey">

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

        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:orientation="vertical">
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:text="Latest Update"
                android:textColor="@color/white"
                android:background="@color/indigo_main"
                android:textSize="10dp"/>
            <Gallery
                android:id="@+id/gallery1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:text="Most Popular"
                android:textColor="@color/white"
                android:background="@color/indigo_main"
                android:textSize="10dp"/>
            <Gallery
                android:id="@+id/gallery2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="New Manga"
            android:textColor="@color/white"
            android:background="@color/indigo_main"
            android:textSize="10dp"/>
        <ListView
                android:drawSelectorOnTop="true"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:id="@+id/lv_comics" />

    </LinearLayout>
</ScrollView>

这是我的布局展示。 http://i.stack.imgur.com/KFIJy.jpg

【问题讨论】:

  • ListViewScrollView ...?不是个好主意。
  • 滚动视图内的列表视图不是一个好主意。为了解决这个问题,您可以将两个视图作为标题添加到列表视图

标签: java android xml listview layout


【解决方案1】:

试试这个动态计算高度

public static void setListViewHeight(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0) {
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
        }
        view.measure(desiredWidth, MeasureSpec.AT_MOST);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

【讨论】:

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