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

一次搞懂 margin 與 padding、layout_gravity 與 gravity 的差別

編輯:關於 Android 程式設計

這幾個屬性在日常開發中幾乎天天用到,但也最容易一時糊塗寫錯。下面用實例把它們的作用範圍徹底分清楚。

一、Layout_Margin 與 padding

1.1 完全不設定

<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>

兩個按鈕緊貼容器邊緣,沒有任何間距。

1.2 在父容器上設定 padding

<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 會往內縮。

1.3 設定 Layout_Margin

<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:控制項內部的內距。設在 TextView/Button 上,文字位置會改變;設在父容器(如 LinearLayout)上,所有子 View 的位置會一起改變。
  • layout_margin:控制項與同層兄弟之間的外距,是控制項之間的相對關係。

一句話記法:padding 是「內容離自己的邊」,margin 是「自己離別人的邊」。

二、gravity 與 layout_gravity

2.1 不設定任何屬性

<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>

2.2 只設定 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:gravity="center"
		android:text="hehe"
		android:textSize="30sp" />
</LinearLayout>

文字被置中到 TextView 內部——gravity 影響的是控制項內部內容的對齊方式。

2.3 只設定 layout_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 影響的是控制項在父容器中的位置。

結論

把兩組屬性放在一起看,規律其實完全一致:

  • padding / gravity:作用對象是「自己內部」。
  • layout_margin / layout_gravity:作用對象是「自己與父容器(或兄弟)之間」。名字裡有 layout_ 前綴的,一律是父容器在解讀的排版資訊。

記住這條判斷規則,之後寫布局時就不會再反覆試算了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved