【发布时间】:2019-10-31 14:21:33
【问题描述】:
当 ListView 包含超过 12 个 ListItem 时,我无法将 ListView 放入弹出对话框。我在这里缺少任何属性还是我必须在后面的代码中自己计算最大尺寸?
当一切都完美契合时,我的布局看起来像这样:
当我有太多项目时,您可以看到列表视图下方的我的 LinearLayout 和 Button 消失。
这是我的 Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/backgroundgradient" android:orientation="vertical">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="3dp" android:background="@drawable/border" android:orientation="vertical">
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:orientation="horizontal">
<TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Caption" android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView style="@style/dialog_label" android:text="test" />
<TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView style="@style/dialog_label" android:text="test" />
<TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView style="@style/dialog_label" android:text="test" />
<TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView style="@style/dialog_label" android:text="test" />
<TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="3dp" android:background="@drawable/border" android:orientation="vertical">
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp">
<TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Caption" android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<ListView android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@android:color/transparent" android:dividerHeight="3dp" android:paddingTop="3dp" android:paddingStart="3dp" android:paddingEnd="1dp" android:choiceMode="singleChoice" android:listSelector="@drawable/selector_list_item"></ListView>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="vertical">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="3dp" android:background="@drawable/border" android:orientation="vertical">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp">
<TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Caption" android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp">
<TextView style="@style/dialog_label" android:text="test" />
<TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView style="@style/dialog_label" android:text="test" />
<TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView style="@style/dialog_label" android:text="test" />
<TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView style="@style/dialog_label" android:text="test" />
<TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView style="@style/dialog_label" android:text="test" />
<TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView style="@style/dialog_label" android:text="test" />
<TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView style="@style/dialog_label" android:text="test" />
<TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView style="@style/dialog_label" android:text="test" />
<TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView style="@style/dialog_label" android:text="test" />
<TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="vertical">
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_weight="1" android:paddingLeft="2dp" android:paddingRight="2dp" android:text="OK" android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
【问题讨论】:
-
无法保证所有内容都能完美适配所有屏幕尺寸,因此您可以将 alignParentBottom="true" 添加到包含按钮的 linearLayout 中。但是,这是一个非常糟糕的设计,我建议您使用带有 RecyclerView 的 ConstraintLayout,这样您的布局文件中的代码将减少大约 75%
-
查看link
-
没有属性“alignParentBottom”。我会试试 ContraintLayout 谢谢。
-
嘿,使用约束布局怎么样?你可以使用约束布局吗?
-
我试试看
标签: android android-listview android-linearlayout android-dialog