1

在佈局文件中,我有一個ListView,其大小可以動態增長/縮小。我有一個按鈕btn_rec_add,它是單擊事件我在ListView中添加一個項目。我已經嘗試了佈局文件中的許多更改,但無法使按鈕根據ListView中的項目數量來移動其位置。如果我將按鈕放在與ListView相同的RelativeLayout中,則按鈕動態移動,這正是我想要的,但在4.3英寸顯示屏手機中添加5個或更多元素後,我看不到按鈕。如果我將按鈕保持在ListView的RelativeLayout之外,則它固定在屏幕上。如何使按鈕的位置基於ListView中項目的數量移動

當前,btn_rec_add固定在佈局的底部。有人可以幫我解決這個問題。

下面是XML代碼:

<?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="match_parent" 
android:background="@drawable/bg" > 

<ImageView 
    android:id="@id/top_bar_view" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:background="@drawable/top_bar" 
    android:contentDescription="@string/content" /> 

<TextView 
    android:id="@+id/txt_recipients" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="8dp" 
    android:padding="8dp" 
    android:text="@string/text_recipients" 
    android:textColor="#FFFFFF" 
    android:textSize="16sp" /> 

<ImageButton 
    android:id="@id/btn_back" 
    android:layout_width="80dp" 
    android:layout_height="50dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:contentDescription="@string/content" 
    android:paddingTop="6dp" 
    android:src="@drawable/ic_back" /> 

<RelativeLayout 
    android:id="@+id/Rlayout_recipients" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/btn_rec_add" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@id/top_bar_view" > 

    <ListView 
     android:id="@+id/rec_list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:divider="@null" 
     android:dividerHeight="0dp" 
     android:paddingTop="20dp" /> 
</RelativeLayout> 

<ImageButton 
    android:id="@+id/btn_rec_add" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:contentDescription="@string/content" 
    android:src="@drawable/icon_add" /> 

</RelativeLayout> 

How it looks currently

回答

0

如果我理解正確的話,你想要的按鈕的行爲如下:

  • 出現下面的最後ListView項目,如果如果ListView擴展的ListView不會擴展到填滿屏幕
  • 屏幕的全高度,按鈕應該是在屏幕的底部,但列表應保持滾動

如果我的理解是正確的,你應該把你ListView和你在LinearLayout按鈕如下:

<LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/button_height" 
     android:background="@drawable/button_image" /> 

</LinearLayout> 

上述佈局的效果如下:

  • 佈局,其中的數據項都垂直放置
  • 佈局,這將是一樣寬父母,但高達ListViewButton
  • ListView將佔用該按鈕不佔用的佈局中的所有空間(這是layout_weight="1"Button沒有佈局權重,從而需要通過@dimen/button_height在這種情況下定義)

大Android的佈局問題,將只需填寫儘可能多的空間!

+0

太棒了!它的作用就像一個魅力:)謝謝:) – user2688158

+0

請確保你瞭解'LinearLayout'發生了什麼。他們是非常有用的佈局!如果您從中瞭解到這一點並解決您的問題,請繼續並提升它的評分:http://meta.stackexchange.com/a/198551/187616 –

-1

你的問題是關係到用戶體驗。 你必須決定用戶是否想要滾動到列表的末尾來按添加按鈕 或用戶想要添加而不滾動到列表的結尾。 由於我們的場景只有兩個選項,所以 要麼保持添加按鈕不變或將其添加爲列表視圖的頁腳。

相關問題