我正在开发 android 中的对话框,我打算使其针对不同的手机分辨率非常灵活。因此,我在构建 xml 文件时使用了 layout_weight 属性。
<?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>
它应该是这样的:
确实看起来我想要它。
但是当我单击底部 Listview 中的项目时,这会将它们从底部 ListView 中删除并将它们添加到左上角的 ListView,底部 Listview 的大小会减小,直到我删除最后一个项目并且它完全消失并且看起来像这个:
我希望它的大小完全不变。即使它是空的。
尽管 dp 是一个非常灵活的尺寸单位,但我不想在这种情况下使用它, 顶部的两个 ListView 应占据屏幕的 3/5,底部的 ListView 占据剩余的 2/5,Button 和 TextView 应在两者之间占据尽可能小的空间。
我试过将最后一个 Listviews 的高度更改为 match_parent,但没有任何改变。当我单击项目时, ListView 仍然消失。
最佳答案
固定最后一个 ListView 的高度,并为左右 ListViews parent 布局设置 android:layout_weight="1"
关于android - 删除项目时,指定权重的 ListView 的大小会减小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13334528/