Specified ListView weight reduces size when item is deleted

I am working on a dialog box in Android, which I am going to make very flexible for different phone resolutions. Therefore, I use the layout_weight attribute when building the XML file.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/newGame_linearLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="0dp" android:layout_gravity="center_horizontal" android:layout_weight="3" android:orientation="horizontal" > <ListView android:id="@+id/newGame_InvitedPlayers" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="5dp" android:layout_weight="1" android:background="@color/ListView_Background" android:cacheColorHint="#00000000" > </ListView> <ListView android:id="@+id/newGame_Plugins" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="5dp" android:layout_weight="1" android:background="@color/ListView_Background" android:cacheColorHint="#00000000" > </ListView> </LinearLayout> <Button android:id="@+id/newGame_sendInvite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Invite Players" /> <TextView android:id="@+id/newGame_textViewOnlinePlayers" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Online Players:" /> <ListView android:id="@+id/newGame_onlinePlayers" android:layout_width="match_parent" android:layout_height="0dp" android:layout_margin="5dp" android:layout_weight="2" android:background="@color/ListView_Background" android:cacheColorHint="#00000000" > </ListView> </LinearLayout> 

Here's how it should look:

desired outcome

And really, it seems like I want it.

But when I click the items in the bottom ListView, which removes them from the bottom ListView and adds them to the top left ListView, the bottom ListView is reduced in size until I delete the very last item and it completely disappears and looks like this:

current outcome

I want this to not change in size at all. Even when it's empty.

Even if dp is a very flexible unit of measure for size, I donโ€™t want to use it in this case, the top two ListViews should occupy 3/5 of the screen, and the bottom will receive the remaining 2/5, and Button and TextView should take up as little space as possible between them .

I tried changing the height of the last Listviews to match_parent, this does not change anything. The list I still disappears when I click items.

+4
source share
1 answer

Correct the height for the last ListView and place the android: layout_weight = "1" for the left and right parent layout of ListViews

0
source

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


All Articles