Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 線性布局 LinearLayout 詳解:gravity、weight 與實戰

編輯:關於 Android 開發

LinearLayout(線性布局)是 Android 中最基礎也最常用的布局之一。本文將重點講解它的兩個核心屬性 gravity 與 weight,並通過實例演示如何實現按比例分配與底部導航等常見效果。

(1)gravity 與 layout_gravity

android:gravity 屬性用於限定該 View 內部內容的位置。比如一個 Button 上面的文字,你可以設置它相對於 View 靠左、靠右等位置。

android:layout_gravity 則用於設置該 View 相對於父 View 的位置。比如一個 Button 位於 LinearLayout 中,想把它放在靠左、靠右等位置,就可以通過該屬性設置。

(2)weight 權重(以水平方向為例)

(a)當 width = 0dp 或 wrap_content 時,按權重比例直接計算。例如下面三個 TextView 權重為 1:2:3:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/text1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/holo_red_dark"
        android:text="Text1"/>
    <TextView
        android:id="@+id/text2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="@android:color/holo_blue_bright"
        android:text="Text2"/>
    <TextView
        android:id="@+id/text3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="@android:color/white"
        android:text="Text3"/>
</LinearLayout>

(b)當 width = match_parent 時,計算方式略有不同。

第一步:三個都為 match_parent 時,總寬度相當於 1 - 3 = -2;

第二步:計算每個 TextView 占有的比例 1/6、2/6、3/6;

第三步:1 - 2×1/6 = 2/3;1 - 2×2/6 = 1/3;1 - 2×3/6 = 0;

第四步:最終比例為 2:1:0。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/holo_red_dark"
        android:text="Text1"
        android:gravity="center"
        android:textSize="40sp"/>
    <TextView
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="@android:color/holo_blue_bright"
        android:text="Text2"
        android:gravity="center"
        android:textSize="40sp"/>
    <TextView
        android:id="@+id/text3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="@android:color/white"
        android:text="Text3"
        android:gravity="center"
        android:textSize="40sp"/>
</LinearLayout>

(3)分割線

可以通過一個寬度較窄的 View 作為分割線:

<View
    android:layout_marginLeft="20sp"
    android:layout_marginRight="20sp"
    android:layout_width="3dp"
    android:layout_height="match_parent"
    android:background="#ff00ee" />

案例:底部導航欄

結合線性布局的權重與嵌套,可以輕鬆實現常見的底部導航欄效果:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80sp"
        android:layout_gravity="bottom"
        android:background="@android:color/holo_purple">
        <LinearLayout
            android:layout_width="0sp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center">
            <ImageView
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:src="@mipmap/ic_launcher"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="one"
                android:textSize="20sp"/>
        </LinearLayout>
        <View
            android:layout_marginLeft="20sp"
            android:layout_marginRight="20sp"
            android:layout_width="3dp"
            android:layout_height="match_parent"
            android:background="#ff00ee" />
        <LinearLayout
            android:layout_width="0sp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center">
            <ImageView
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:src="@mipmap/ic_launcher"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="two"
                android:textSize="20sp"/>
        </LinearLayout>
        <View
            android:layout_marginLeft="20sp"
            android:layout_marginRight="20sp"
            android:layout_width="3dp"
            android:layout_height="match_parent"
            android:background="#ff00ee" />
        <LinearLayout
            android:layout_width="0sp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center">
            <ImageView
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:src="@mipmap/ic_launcher"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="three"
                android:textSize="20sp"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved