簡體   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