[英]Buttons below ListView are not fixed to the bottom of the screen when the ListView is empty
我有一個布局,其中包含一個ListView下面的一些按鈕。
我想要的是將按鈕固定在屏幕底部。
當ListView有多個項目(超出屏幕顯示的大小)時,代碼將正常工作,並且按鈕會出現在屏幕底部。
但是,當ListView為空或不需要滾動時,按鈕將出現在屏幕上(它們並沒有固定在底部)。
這是布局(省略了一些細節,但如有必要,我可以添加它們):
<LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView />
<ListView
android:width="fill_parent"
android:height="wrap_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|bottom"
android:orientation="horizontal"
android:layout_weight="0" />
<Button
android:layout_weight="1" />
<Button
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
我希望將第二個LinearLayout的“重力”設置為“底部”,以使按鈕固定在屏幕底部,但在上述情況下不起作用。
有任何想法嗎?
謝謝。
您將頂部LinearLayout的layout_height設置為wrap_content。 這意味着它將隨着內容的收縮而收縮。 嘗試將其設置為fill_parent。 或者,將其layout_weight設置為1。
選項之一是創建TextView,當它為空時將顯示而不是listview。 您可以使用listView.setEmptyView(emptyView)
然后將此TextView的layout_weight設置為與listview相同的權重。 因此,當listview為空時,其位置將由emptyView占據,並且按鈕將固定在底部。 還將您的emptyView設置為android:gravity="center"
。
其他選擇:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.