【发布时间】:2015-10-02 09:31:51
【问题描述】:
我有一个带有列表视图、EditText 和两个按钮(接受和取消)的简单片段对话框。
我希望我的布局如下所示: 列表视图在顶部 EditText 就在下面 和 在edittext下方并排的按钮。
现在我的问题是 listview 可以有 0 或 100 个元素。 因此,如果我将所有这些都放在一个垂直的 LinearLayout 中,并且 listview 有很多元素,那么 edittext 和按钮就会被推到视野之外。如果我使用相对布局并说编辑文本与父底部对齐并在列表视图下方,对于 100 个元素来说它看起来不错,但是当列表视图为空时,对话框会使用所有屏幕空间。
这是我使用 relativeLayout 时的代码示例。是否有另一种方法可以使 id 为 "below" 的 linearLayout 在列表视图下方,但在不使用 alignParentBottom="true" 的情况下仍然可见(如果列表视图有很多项目),因为这就是布局延伸到的原因全屏。
<?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:orientation="vertical" >
<ListView
android:id="@+id/ListViewPreNotes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/bottom" />
<LinearLayout
android:id="@+id/below"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:padding="3dp">
<TextView
android:layout_height="@dimen/label_height"
android:layout_width="130dip"
android:text="@string/note"
android:layout_marginLeft="10dip"
android:gravity="center_vertical"
android:textSize="@dimen/text_size_large"/>
<EditText
android:id="@+id/OMnote"
android:layout_height="wrap_content"
android:layout_width="200dp"
android:textSize="@dimen/text_size_large"
android:inputType="textNoSuggestions|textCapSentences"
android:selectAllOnFocus="true"
android:hint="@string/note"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:baselineAligned="false">
<Button
android:id="@+id/dialogButtonClose"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="@string/ok"
style="?android:attr/borderlessButtonStyle"
android:textStyle="bold" />
<Button
android:id="@+id/dialogButtonCancel"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="@string/cancel"
android:textStyle="bold"
style="?android:attr/borderlessButtonStyle" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
【问题讨论】:
标签: android android-layout android-listview android-linearlayout android-relativelayout