2015-04-27 63 views
1

我想在屏幕上看到一個列表,並在其下面顯示一個按鈕,但該按鈕應該位於列表的下方並始終可見,無論列表大小如何,並且不管該列表是否具有滾動條,因爲它對於屏幕來說太大。該列表應該在屏幕的頂部。我現在有是這樣的:Android中ListView底部的小部件

<?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"> 
    <ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:choiceMode="singleChoice" 
    android:layout_above="@+id/btn"/> 
    <Button 
    android:id="@+id/btn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="button" 
    android:layout_alignParentBottom="true"/> 
</RelativeLayout> 

如果該列表是大的(所以它需要比它的屏幕空間更多),那麼它得到一個滾動條,一切都很好。我不喜歡的是當列表很短時會發生什麼:在屏幕的頂部有幾個列表元素,然後在底部有按鈕。如果我將列表的layout_height切換到wrap_content並調整一些參數,那麼按鈕就會在列表的正下方,就像我想要的那樣。但是,用這個佈局,如果列表很大,按鈕將不可見:

<?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"> 
    <ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:choiceMode="singleChoice" 
    android:layout_alignParentTop="true"/> 
    <Button 
    android:id="@+id/btn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/list" 
    android:text="button"/> 
</RelativeLayout> 

所以我想如果有一種方法只是在XML來實現這個「近列表按鈕」。我傾向於認爲這是不可能的,但我對Android佈局沒有很強的把握。去Java,你可以計算各種東西並調整佈局,但這似乎很容易出錯,所以我最終可能會做的就是把按鈕放在右邊。

回答

0

看一看這個

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="48dp" 
     android:choiceMode="singleChoice" /> 

    <Button 
     android:id="@+id/btn" 
     android:layout_width="wrap_content" 
     android:layout_height="48dp" 
     android:layout_alignBottom="@id/list" 
     android:text="button" /> 
</RelativeLayout> 

注意,這裏設置爲ListView的填充按鈕的固定高度。對於這兩個維度,您應該從dimen.xml中引用相同的值。

+0

謝謝,這實際上看起來應該是這樣。唯一的改進是從某個地方得到「48dp」而不是硬編碼它,dimens.xml或不。 – ciobi

+0

是的,這是缺點..但如果你想用純XML定義的佈局,我沒有看到其他方式。除非你的按鈕需要多線,否則這應該不成問題。 – Lamorak

0

as @ assad的建議,有一個線性佈局,並將其分爲10個部分。第一部分標題,2-9部分爲listview,第十部分爲button。

+0

謝謝,但按鈕應該始終可見,而不是列表中的最後一件事。 – ciobi

+0

看到我的編輯ANS – kiturk3

1

試試這個

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentTop="true" 
    android:layout_weight="1.0" 
    android:choiceMode="singleChoice" /> 

<Button 
    android:id="@+id/btn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/list" 
    android:layout_gravity="right" 
    android:text="button" /> 

</LinearLayout> 
+0

按鈕仍處於屏幕採用了4元素的列表,而不是僅僅4號線下方的底 – ciobi

+0

我有你的觀點,現在首先計算你的列表視圖大小比你的ListView這將設定最高HIGHT幫你。 http://www.java2s.com/Code/Android/UI/setListViewHeightBasedOnChildren.htm @ciobi – Assad

+0

謝謝,我會考慮一下。也許有人有一個XML解決方案,雖然... – ciobi

0

列表設置爲layout_alignParentTop和layout_above按鈕 和按鈕,將其設置爲layout_alignParentBottom。

<?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"> 

    <ListView 
     android:layout_above="@+id/btn" 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:choiceMode="singleChoice" 
     android:layout_alignParentTop="true" /> 

    <Button 
     android:id="@+id/btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:text="button" /> 
</RelativeLayout> 

enter image description here

+0

這是好的,但listview項將顯示在按鈕旁邊,因爲按鈕不在列表視圖下方,它是列表視圖的頂部。我建議線性佈局 – Assad

0

創建一個頁腳視圖佈局由您的按鈕,並添加

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

希望它能幫助!

+0

謝謝,但按鈕應該始終在屏幕上,而addFooterView()把它作爲列表中的最後一個元素。如果列表很大,除非您滾動,否則無法看到該按鈕。 – ciobi

0

可以使用footerView爲此,你的主要的XML更改爲僅包含列表視圖

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

<ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:choiceMode="singleChoice" /> 

</RelativeLayout> 

現在,創建像另一個佈局footer_button.xml下面

<?xml version="1.0" encoding="utf-8"?> 
    <Button xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/hello_world" > 
</Button> 

現在在你的活動代碼

ListView lv = (ListView) findViewById(R.id.list); 
    View b1 = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_button, null, false); 
    lv.addFooterView(b1); 

注意:在將適配器設置爲您的列表視圖之前添加頁腳視圖

+0

是的,按鈕不是隱藏的,但列表只顯示一行一半,然後是下面的按鈕,然後是大量的空白屏幕空間。 – ciobi

+0

我明白了,我已經更新了答案,試試 – user2400432