[Android] Android 让UI控件固定于底部的几种方法

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

扫描二维码关注公众号,回复: 5790968 查看本文章

本文原文地址:https://www.cnblogs.com/wukong1688/p/10660549.html

转载请著名出处!谢谢~~

猜你喜欢

转载自www.cnblogs.com/wukong1688/p/10660549.html