編輯:關於 Android 程式設計
這幾個屬性在日常開發中幾乎天天用到,但也最容易一時糊塗寫錯。下面用實例把它們的作用範圍徹底分清楚。
<LinearLayout android:id="@+id/groupView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#22aaa4" android:orientation="vertical" xmlns:android="https://schemas.android.com/apk/res/android"> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="100dp" android:background="#9944aa" android:text="hehe" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:text="hehe2" /> </LinearLayout>
兩個按鈕緊貼容器邊緣,沒有任何間距。

<LinearLayout android:id="@+id/groupView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#22aaa4" android:orientation="vertical" android:padding="50dp" xmlns:android="https://schemas.android.com/apk/res/android"> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="100dp" android:background="#9944aa" android:text="hehe" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:text="hehe2" /> </LinearLayout>

可以看到 LinearLayout 內部所有子控制項都往內縮了一段距離。接著在父容器有 padding 的前提下,替第二個 Button 額外加上 paddingTop:
<LinearLayout android:id="@+id/groupView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#22aaa4" android:orientation="vertical" android:padding="50dp" xmlns:android="https://schemas.android.com/apk/res/android"> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="100dp" android:background="#9944aa" android:text="hehe" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:paddingTop="40dp" android:text="hehe2" /> </LinearLayout>

差別就很清楚了:padding 影響的是「設定了這個屬性的控制項,其內部內容的起始位置」。設在 Button 上,文字會往內縮;設在 LinearLayout 上,所有子 View 會往內縮。
<LinearLayout android:id="@+id/groupView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#22aaa4" android:orientation="vertical" xmlns:android="https://schemas.android.com/apk/res/android"> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="100dp" android:layout_margin="100dp" android:background="#9944aa" android:text="hehe" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:text="hehe2" /> </LinearLayout>

再來一張,替第二個按鈕改用 layout_marginTop:
<LinearLayout android:id="@+id/groupView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#22aaa4" android:orientation="vertical" xmlns:android="https://schemas.android.com/apk/res/android"> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="100dp" android:layout_margin="100dp" android:background="#9944aa" android:text="hehe" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="100dp" android:text="hehe2" /> </LinearLayout>

注意這裡 margin 是「往外推」,把自己與其他兄弟控制項拉開距離——而不是把自己在父容器中的相對位置往某個方向移動。第二張圖正好說明了這點。
一句話記法:padding 是「內容離自己的邊」,margin 是「自己離別人的邊」。
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#44aa77" xmlns:android="https://schemas.android.com/apk/res/android"> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:background="#aa2288" android:text="hehe" android:textSize="30sp" /> </LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#44aa77" xmlns:android="https://schemas.android.com/apk/res/android"> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:background="#aa2288" android:gravity="center" android:text="hehe" android:textSize="30sp" /> </LinearLayout>

文字被置中到 TextView 內部——gravity 影響的是控制項內部內容的對齊方式。
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#44aa77" xmlns:android="https://schemas.android.com/apk/res/android"> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:background="#aa2288" android:layout_gravity="center" android:text="hehe" android:textSize="30sp" /> </LinearLayout>

整個 TextView 在父容器中改變了位置,但文字在其內部的對齊並未改變——layout_gravity 影響的是控制項在父容器中的位置。
把兩組屬性放在一起看,規律其實完全一致:
layout_ 前綴的,一律是父容器在解讀的排版資訊。記住這條判斷規則,之後寫布局時就不會再反覆試算了。
ButterKnife 視圖注入庫的完整使用指南
講到「注入」,相信 Android 開發者都不陌生。它不僅能讓程式碼結構更精簡,還能大幅減少 findViewById 這類冗長的程式碼。平常我們看到的注入功能,多半是封
《Android源碼設計模式解析與實戰》讀書筆記(十八):代理模式
第十八章、代理模式 代理模式也稱委託模式,是結構型設計模式之一,也是應用最廣泛的模式之一。 1. 定義 為其他物件提供一種代理,以控制對該物件的存取。 2. 使用場景 當
微信怎麼加好友?添加好友失敗的原因與解法
微信怎麼加好友,對多數人來說不是難事;真正讓人頭痛的是「明明資料都填對了,卻一直加不上」。這個問題通常不是手機壞了,而是被某個環節擋住。以下先快速回顧常見的加好友方式,再
Android 泡泡聊天介面實作教學(附完整程式碼)
本文透過一個完整範例,帶大家動手打造類似即時通訊軟體的「泡泡」對話介面。點擊發送按鈕後,輸入的文字會立刻以對話框的形式呈現在畫面上。 這個實作是在網路上既有範例的基礎上