Set maximum height for list

Something is really stupid, but I want to set the maximum height for my list, and I don't seem to find something that works. Some talk about setting maxheight for a list, but I can’t find this option? Now i have

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="40dp" > <ListView android:id="@+id/lvSwitch" android:layout_width="fill_parent" android:layout_height="120dp" > </ListView> </LinearLayout> 

But when there is only one element in the list, it still has a height of 120dp, which, of course, is not necessary, it simply can not exceed this height ...

+4
source share
3 answers

Try the following:

  android:layout_height="wrap_content". 

Edit: you can check the number of items in your list and set the size if it is above 20. Something like:

 if(numberOfItems>20){ listview.setWidth(yourSize) } 
+1
source
 android:layout_height="0dp" android:layout_weight="1" 
+1
source

Along with or instead of android:layout="120dp" try android:minHeight="20dp" , assuming 20dp is the height of one list item. That way, if a ListView has 1 or 2 things, it won't take just 120dp.

0
source

Source: https://habr.com/ru/post/1446275/


All Articles