简体   繁体   中英

Android list view wrap content until a certain size based on footer size

I am working on an android application.

In an XML Layout I need to do the following:

I have a list view at the top (listViewProducts) , and another Relative view under it (receiptSection).

The list view should take as much space as it has items. And the rest is taken by the receiptSection.

So for example if I have 2 items in the listViewProducts:

第一张图片

The list view is as big as the 2 items and the rest is taken by the receiptView.

If I add another item, the list view now take more space and push the receiptView lower:

在此输入图像描述

However if I add a lot more items, I want the list view height to stop growing to leave a minimum height for the receiptView that cannot go smaller:

在此输入图像描述

As you see in the picture, the receiptVIew has a minimum height of 50dp. once the receipt view get to that height, it should stop shrinking and now the list view has a fixed size based on the remaining of the space. The rest will be scrollable.

What I have tried

I created a list view. I have android:layout_alignParentTop="true" and android:layout_height="wrap_content" .

This will make it grow with its content and its at the top of the view

<ListView
    android:id="@+id/listViewProducts"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" >
</ListView>

THen I created a RelativeLayout that will hold the checkout_receipt_view that is in a seperate xml layout file.

For this view I have android:layout_alignParentBottom="true" and android:layout_below="@id/listViewProducts" that will make it go under the list view and align with the bottom of the view.

I also used android:minHeight="50d" in order to set the minimum height of the receiptSection.

<RelativeLayout
    android:id="@+id/receiptSection"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/listViewProducts"
    android:minHeight="50dp" >
    <include layout="@layout/checkout_receipt_view" />
</RelativeLayout>

The listViewProducts is growing with the items, and the receiptView is taking the remaining space correctly.

The problem however the minimum height did not work. The list view keeps on growing infinitely and the receiptSection will be pushed out of the view.

Is there a way I can make the listView stop growing when the receiptView reaches 50dp?

Thanks a lot for any help.

Try swapping the 'layout_below'.

What you are actually saying is the following: please put my relativelayout BELOW the listview. If you want your listview to respect the height of the relativelayout, you'll have to say in the listview:

 <ListView
    android:id="@+id/listViewProducts"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/receiptSection"
    android:layout_alignParentTop="true" >
</ListView>

And your relativelayout:

<RelativeLayout
    android:id="@+id/receiptSection"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:minHeight="50dp" >
    <include layout="@layout/checkout_receipt_view" />
</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM