【发布时间】:2012-10-31 08:01:41
【问题描述】:
我正在开发 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,底部的一个占据剩余的 2/5,而 Button 和 TextView 之间应该占据尽可能小的空间。
我已经尝试将最后一个 Listviews 高度更改为 match_parent,它不会改变任何东西。单击项目时列表视图仍然消失。
【问题讨论】:
-
我有理由确定这种行为是因为对话框试图最小化其所需大小的方式,因此它的内容。基本上,高度是被包裹的,您可以通过直观地比较期望结果和实际结果来轻松判断。要覆盖此标准行为,您可以尝试操作对话框所附加的
Window的LayoutParams。 See here for an example。或者,您可以设置一个Fragment或Activity并显示对话框。 -
我已经试过了。奇怪的是,它不会保持相同的大小。它的反应方式相同,只有一个区别。当对话框变小时,我在底部遇到了一个图形问题。我到达那里的“开始游戏”按钮和“取消”按钮如下所示:junk.zomis.net/stackoverflow/layoutparams.png
-
我尝试用一个活动来设置它,它就像我想要的那样工作。谢谢你。如果您将其写为答案,我可以接受它作为将来其他人看到的最佳答案。
标签: android listview android-linearlayout android-layout-weight