Android - ListView的高度恰好适合1个ListView项目

时间:2015-06-25 04:10:10

标签: java android xml listview layout

我正在使用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

1 个答案:

答案 0 :(得分:0)

尝试这个动态计算高度

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();
}
相关问题