我想在屏幕上看到一個列表,並在其下面顯示一個按鈕,但該按鈕應該位於列表的下方並始終可見,無論列表大小如何,並且不管該列表是否具有滾動條,因爲它對於屏幕來說太大。該列表應該在屏幕的頂部。我現在有是這樣的: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,你可以計算各種東西並調整佈局,但這似乎很容易出錯,所以我最終可能會做的就是把按鈕放在右邊。
謝謝,這實際上看起來應該是這樣。唯一的改進是從某個地方得到「48dp」而不是硬編碼它,dimens.xml或不。 – ciobi
是的,這是缺點..但如果你想用純XML定義的佈局,我沒有看到其他方式。除非你的按鈕需要多線,否則這應該不成問題。 – Lamorak