对话框动态布局中的列表视图

Listview in dialog dynamic layout

我有一个带有列表视图、EditText 和两个按钮(接受和取消)的简单片段对话框。

我希望我的布局如下所示: 顶部的列表视图 EditText 就在下方 和 按钮并排在 edittext 下方。

现在我的问题是列表视图可以有 0 个或 100 个元素。 因此,如果我将 everythis 放在一个垂直的 LinearLayout 中,并且 listview 有很多元素,那么 edittext 和按钮就会被推出视图。如果我使用相对布局并说编辑文本与父底部对齐并位于列表视图下方,它看起来适合 100 个元素但是当列表视图为空时对话框使用所有屏幕 space.

这是我使用 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>

您可以使用 FrameLayout 作为根,然后您可以将 LinearLayouts 放在 ListView 的顶部。 http://developer.android.com/reference/android/widget/FrameLayout.html

当没有找到记录时将 ListView.Visibility 设置为 Gone 并使用 RelativeLayout 并对齐父级底部。

您可以在其页脚的 ListView 下方添加您需要显示的内容。你可以像这样添加它。

View footerView = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
ListView.addFooterView(footerView);

一定要感谢 Arsalan Shah 的建议。

这是我解决问题的最终版本(有点老套):

首先,我检查列表视图中有多少元素,然后根据该元素以及设备和方向模式(纵向或横向),我将问题中的布局或带有 Arsalan 的列表视图页脚的其他布局膨胀沙阿建议道。

示例: 如果设备小于 7 英寸并且处于横向模式并且我列表中的项目数量超过 4 我将使用我的布局否则我将使用 Arsalan Shah 建议。我测试了 layout/device 应该是什么布局才能为我的设计找到最佳案例场景。

希望这对可能遇到同样问题的其他人有所帮助。 如果有人建议如何仅在一种布局中完成所有这些操作,请在下方发表评论。