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

android:gravity 屬性用於限定該 View 內部內容的位置。比如一個 Button 上面的文字,你可以設置它相對於 View 靠左、靠右等位置。
android:layout_gravity 則用於設置該 View 相對於父 View 的位置。比如一個 Button 位於 LinearLayout 中,想把它放在靠左、靠右等位置,就可以通過該屬性設置。
(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>
可以通過一個寬度較窄的 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>
Android Studio 中使用 OpenCV 的三種方式詳解
告別 OpenCV Manager 的三種整合方案 OpenCV 是強大的電腦視覺庫,在 Android 上也有廣泛應用。最早接觸它時還是 Eclipse 時代,那時網上
仿 QQ 5.0 側滑菜單實作:SlidingMenu 的視差縮放與手勢判定
做法概覽 側滑菜單的核心是「一個可以水平滾動的容器 + 兩個並排的區塊」。這裡繼承 HorizontalScrollView,內部放一個 LinearLayout,裡面依
仿 QQ 側滑選單實作:Fragment 切換、回調機制與網路 JSON 解析一次學會
仿 QQ 側滑選單實作:Fragment 切換、回調機制與網路 JSON 解析一次學會 這篇把一個完整的側滑選單範例拆開來講,涵蓋的知識點不少:Fragment 的動態切
Android 快樂貪吃蛇實戰:用 Canvas 與 Path 繪製三角形方向鍵(03)
繪製自定義圖形的基本思路 在 Android 中,控件的視覺效果通常都需要通過繪製完成。自定義 View 的核心之一,就是重寫 onDraw 方法,在系統提供的 Canv