【问题标题】:Listview in dialog dynamic layout对话框动态布局中的Listview
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 是的,我做到了,但我不想给它重量,因为我仍然希望它根据列表视图中的元素动态更改大小,但我希望按钮和编辑文本始终显示
    • 检查更新的答案。如果您将列表视图可见性设置为 GONE,它将不再存在于对话框中,并且其高度将使用 edittext 和按钮自动调整
    • 这会起作用,但只有在没有项目的情况下才能工作。如果有 2 个怎么办? ://
    • 你可以使用 IF ELSE 吗? If(listview has no items) ListView.Visibility to Gone Else ListView.Visibility to Visible
    【解决方案2】:

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

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

    【讨论】:

    • 这是一个不错的解决方案,但不是我所需要的。因为如果有 100 个项目,我需要一直向下滚动,只需单击接受:/
    • 是的,这个解决方案有这样的限制。另一种方法是,您将页脚保持在 ListView 上方,并在运行时将页脚的上边距作为 ListView 的高度,并检查不应允许上边距使页脚超出视图边界。跨度>
    【解决方案3】:

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

    【讨论】:

    • 但我不希望它在上面。这个问题:stackoverflow.com/questions/7716635/… 有我想要的图片示例,但我希望它在对话片段中,如果不需要,对话片段不会拉伸
    【解决方案4】:

    感谢 Arsalan Shah 的建议。

    这是我解决问题的最终版本(有点骇人听闻):

    首先,我检查列表视图中有多少元素,然后根据设备和方向模式(纵向或横向)对问题中的布局或 Arsalan Shah 建议的带有列表视图页脚的其他布局进行膨胀.

    示例: 如果设备低于 7 英寸并且处于横向模式并且列表中的项目数高于 4,我将使用我的布局,否则我将使用 Arsalan Shah 的建议。我测试了哪种布局上的项目数/device 应该用于什么布局,以便为我的设计找到最佳案例。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多