底部ListView的空间

时间:2016-05-25 15:05:54

标签: java android listview

问题是我需要这样做,内部是ScrollView ListView,他和scrollview滚动。这是我如何实现的,但在ListView和第三个容器之间的大空间。

enter image description here

main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Details.Book.BookDetails"
android:background="@color/fragment_bg">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"/>

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/scrollView6"
    android:layout_below="@+id/toolbar">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_below="@+id/toolbar"
            android:id="@+id/linearLayout16">

            <RelativeLayout
                android:id="@+id/relativeLayout9"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:background="@drawable/rounded_top_layout"
                android:paddingBottom="@dimen/leftPadding"
                android:paddingLeft="@dimen/rightPadding"
                android:paddingRight="@dimen/rightPadding"
                android:paddingTop="@dimen/leftPadding">

                <TextView
                    android:id="@+id/textviewTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="false"
                    android:layout_centerVertical="true"
                    android:text="@string/buy_det_pass_txt"
                    android:textColor="@color/background_color"
                    android:textSize="@dimen/mainLargeSize" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/relativeLayout10"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/relativeLayout9"
                android:paddingBottom="@dimen/leftPadding"
                android:paddingLeft="@dimen/rightPadding"
                android:paddingRight="@dimen/rightPadding"
                android:paddingTop="@dimen/leftPadding">

                <LinearLayout
                    android:id="@+id/linearLayout4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/relativeLayout9"
                    android:layout_marginBottom="@dimen/textMargin"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/textView43"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/buy_det_fullname"
                        android:textColor="@color/mainGreyColor"
                        android:textSize="@dimen/mainSmallSize" />

                    <TextView
                        android:id="@+id/tvFullName"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="right"
                        android:text="Medium Text"
                        android:textColor="@color/mainGreyColor"
                        android:textSize="@dimen/mainMiddleSize" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/linearLayout3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/linearLayout4"
                    android:layout_marginBottom="@dimen/textMargin"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/textView35"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/buy_det_gender"
                        android:textColor="@color/mainGreyColor"
                        android:textSize="@dimen/mainSmallSize" />

                    <TextView
                        android:id="@+id/tvGender"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="right"
                        android:text="Medium Text"
                        android:textColor="@color/mainGreyColor"
                        android:textSize="@dimen/mainMiddleSize" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/linearLayout6"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/linearLayout3"
                    android:layout_marginBottom="@dimen/textMargin"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/textView37"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/buy_det_ticknumber"
                        android:textColor="@color/mainGreyColor"
                        android:textSize="@dimen/mainSmallSize" />

                    <TextView
                        android:id="@+id/tvTickNumber"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="right"
                        android:text="Medium Text"
                        android:textColor="@color/mainGreyColor"
                        android:textSize="@dimen/mainMiddleSize" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/linearLayout7"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/linearLayout6"
                    android:layout_marginBottom="@dimen/textMargin"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/textView39"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/buy_det_issdate"
                        android:textColor="@color/mainGreyColor"
                        android:textSize="@dimen/mainSmallSize" />

                    <TextView
                        android:id="@+id/tvIssDate"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="right"
                        android:text="Medium Text"
                        android:textColor="@color/mainGreyColor"
                        android:textSize="@dimen/mainMiddleSize" />
                </LinearLayout>

            </RelativeLayout>

        </LinearLayout>

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listView"
            android:layout_centerHorizontal="true"
            android:paddingRight="@dimen/textMargin"
            android:paddingLeft="@dimen/textMargin"
            android:layout_below="@+id/linearLayout16" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_below="@+id/listView">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:background="@drawable/rounded_top_layout"
                android:id="@+id/relativeLayout9"
                android:paddingTop="@dimen/leftPadding"
                android:paddingBottom="@dimen/leftPadding"
                android:paddingLeft="@dimen/rightPadding"
                android:paddingRight="@dimen/rightPadding">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/buy_det_cost_txt"
                    android:id="@+id/tvFromTo"
                    android:textColor="@color/background_color"
                    android:textSize="@dimen/mainLargeSize"
                    android:layout_alignParentTop="false"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_centerVertical="true" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/relativeLayout9"
                android:id="@+id/relativeLayout10"
                android:paddingRight="@dimen/rightPadding"
                android:paddingLeft="@dimen/rightPadding"
                android:paddingTop="@dimen/leftPadding"
                android:paddingBottom="@dimen/leftPadding">

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/relativeLayout9"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:id="@+id/linearLayout4"
                    android:gravity="center_vertical"
                    android:layout_marginBottom="@dimen/textMargin" >

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/buy_det_fare"
                        android:id="@+id/textView43"
                        android:layout_weight="1"
                        android:textColor="@color/mainGreyColor"
                        android:textSize="@dimen/mainSmallSize" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Medium Text"
                        android:id="@+id/tvFare"
                        android:layout_weight="1"
                        android:textColor="@color/mainGreyColor"
                        android:gravity="right"
                        android:textSize="@dimen/mainMiddleSize" />
                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/linearLayout4"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:id="@+id/linearLayout3"
                    android:gravity="center_vertical"
                    android:layout_marginBottom="@dimen/textMargin">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/buy_det_taxes"
                        android:id="@+id/textView35"
                        android:layout_weight="1"
                        android:textColor="@color/mainGreyColor"
                        android:textSize="@dimen/mainSmallSize" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Medium Text"
                        android:id="@+id/tvTaxes"
                        android:layout_weight="1"
                        android:textColor="@color/mainGreyColor"
                        android:gravity="right"
                        android:textSize="@dimen/mainMiddleSize" />
                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/linearLayout3"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:id="@+id/linearLayout6"
                    android:gravity="center_vertical"
                    android:layout_marginBottom="@dimen/textMargin">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/buy_det_service_fees"
                        android:id="@+id/textView37"
                        android:layout_weight="1"
                        android:textColor="@color/mainGreyColor"
                        android:textSize="@dimen/mainSmallSize" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Medium Text"
                        android:id="@+id/tvFees"
                        android:layout_weight="1"
                        android:textColor="@color/mainGreyColor"
                        android:gravity="right"
                        android:textSize="@dimen/mainMiddleSize" />
                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/linearLayout6"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:id="@+id/linearLayout7"
                    android:gravity="center_vertical"
                    android:layout_marginBottom="@dimen/textMargin">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/buy_det_total_price"
                        android:id="@+id/textView39"
                        android:layout_weight="1"
                        android:textColor="@color/mainGreyColor"
                        android:textSize="@dimen/mainSmallSize" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Medium Text"
                        android:id="@+id/tvTotPrice"
                        android:layout_weight="1"
                        android:textColor="@color/mainGreyColor"
                        android:gravity="right"
                        android:textSize="@dimen/mainMiddleSize" />
                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/linearLayout7"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:id="@+id/linearLayout15"
                    android:gravity="center_vertical"
                    android:layout_marginBottom="@dimen/textMargin" >

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/buy_det_form_of_pay"
                        android:id="@+id/textView54"
                        android:layout_weight="1"
                        android:textColor="@color/mainGreyColor"
                        android:textSize="@dimen/mainSmallSize" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Medium Text"
                        android:id="@+id/tvPayment"
                        android:layout_weight="1"
                        android:textColor="@color/mainGreyColor"
                        android:gravity="right"
                        android:textSize="@dimen/mainMiddleSize" />
                </LinearLayout>

            </RelativeLayout>

        </LinearLayout>
    </LinearLayout>
</ScrollView>
</RelativeLayout>

MainActivity.java

ListView listView = (ListView)findViewById(R.id.listView);
ListUtils.setDynamicHeight(listView);

ListUtils类

public static class ListUtils {
    public static void setDynamicHeight(ListView mListView) {
        ListAdapter mListAdapter = mListView.getAdapter();
        if (mListAdapter == null) {
            return;
        }
        int height = 0;
        int desiredWidth = View.MeasureSpec.makeMeasureSpec(mListView.getWidth(), View.MeasureSpec.UNSPECIFIED);
        for (int i = 0; i < mListAdapter.getCount(); i++) {
            View listItem = mListAdapter.getView(i, null, mListView);
            listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            height += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = mListView.getLayoutParams();
        params.height = height + (mListView.getDividerHeight() * (mListAdapter.getCount() - 1));
        mListView.setLayoutParams(params);
        mListView.requestLayout();
    }
}

0 个答案:

没有答案