歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 如何置底一個View

Android 如何置底一個View

日期:2017/3/1 10:36:53   编辑:Linux編程

【Android 如何置底一個View(附 前置聲明layout布局文件中的資源ID)】 。今天在考慮一個RelativeLayout布局,整個屏幕分為兩個部分,上部分是一個ListView,下部分是兩個橫排的Button。欲實現這兩個Button始終置底,ListView在Button的上方占滿剩余的空間。

Button置底這個方法還算簡單,直接將兩個Button包裹於一個LinearLayout,然後設置這個LinearLayout的屬性android:layout_alignParentBottom為true即可。

效果如下:

XML代碼如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <!-- Button 置底 -->
  6. <LinearLayout android:id="@+id/test_bottom_buttons"
  7. android:layout_width="fill_parent" android:layout_height="wrap_content"
  8. android:orientation="horizontal" android:layout_alignParentBottom="true">
  9. <Button android:layout_width="wrap_content"
  10. android:layout_height="wrap_content" android:text="確定"></Button>
  11. <Button android:layout_width="wrap_content"
  12. android:layout_height="wrap_content" android:text="取消"></Button>
  13. </LinearLayout>
  14. </RelativeLayout>

接下來就是要把剩余的空間用一個ListView進行填充了。

最開始bill臆斷地認為,只要在包裹Buttons的LinearLayout代碼上方加上一個ListView就OK了,這是我最開始錯誤的xml布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <ListView android:id="@+id/lv_test" android:layout_width="fill_parent"
  6. android:layout_height="fill_parent">
  7. </ListView>
  8. <!-- Button 置底 -->
  9. <LinearLayout android:id="@+id/test_bottom_buttons"
  10. android:layout_width="fill_parent" android:layout_height="wrap_content"
  11. android:orientation="horizontal" android:layout_alignParentBottom="true">
  12. <Button android:layout_width="wrap_content"
  13. android:layout_height="wrap_content" android:text="確定"></Button>
  14. <Button android:layout_width="wrap_content"
  15. android:layout_height="wrap_content" android:text="取消"></Button>
  16. </LinearLayout>
  17. </RelativeLayout>
Copyright © Linux教程網 All Rights Reserved