繁体   English   中英

对话框动态布局中的Listview

[英]Listview in dialog dynamic layout

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

我希望我的布局看起来像这样:Listview位于EditText顶部的正下方,而Button并排位于edittext的下方。

现在我的问题是listview可以有0或100个元素。 因此,如果我将一切都放置在垂直的LinearLayout中,并且列表视图中包含很多元素,则编辑文本和按钮会被推出视图。 而且,如果我使用相对布局,并说编辑文本在列表视图的父底部和底部对齐,则对于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>

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

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

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

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

值得称赞的是Arsalan Shah的建议。

这是我如何解决问题的最终版本(有点hacky):

首先,我检查列表视图中有多少个元素,然后根据该列表以及设备和方向模式(纵向或横向),用我的问题或Arsalan Shah建议的列表视图页脚来填充问题的布局。

示例:如果设备低于7英寸,并且处于横向模式,并且列表中的项目数高于4,则我将使用自己的布局,否则将使用Arsalan Shah建议。我已经测试了项目的数量哪种布局/设备应该适合哪种布局,才能为我的设计找到最佳情况。

希望这对其他可能遇到相同问题的人有所帮助。 如果有人对如何仅以一种布局进行所有操作提出建议,请在下面评论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM