Android 讓UI控件固定於底部的幾種方法
1.采用linearlayout布局:
android:layout_height="0dp" <!-- 這里不能設置fill_parent -->
android:layout_weight="1" <!-- 這里設置layout_weight=1是最關鍵的,否則底部的LinearLayout無法到底部 -->
2. 采用relativelayout布局:
android:layout_alignParentBottom="true" <!-- 這里設置layout_alignParentBottom=true是最關鍵的,這個屬性上級必須是RelativeLayout -->
=====================================
布局xml代碼如下:
1)采用linearlayout布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp" <!-- 這里不能設置fill_parent -->
android:layout_weight="1" <!-- 這里設置layout_weight=1是最關鍵的,否則底部的LinearLayout無法到底部 -->
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:focusable="false" />
</LinearLayout>
</LinearLayout>
2)采用relativelayout布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" <!--上級必須是RelativeLayout-->
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000"
android:focusable="false"
android:text="button"/>
</LinearLayout>
</RelativeLayout>
本博客地址: wukong1688
本文原文地址:https://www.cnblogs.com/wukong1688/p/10660549.html
轉載請著名出處!謝謝~~