標籤:android padding layout_margin
林炳文Evankaka原創作品。轉載請註明出處http://blog.csdn.net/evankaka
一、定義
android:layout_margin就是設定view的上下左右邊框的額外空間
android:padding是設定內容相對view的邊框的距離
padding,含義為“填充”,像墊肩壓類似的填充物,一個控制項的padding及此控制項內部的填充,由此可見padding是以所被定義的控制項A為parent控制項,而內部的內容物與控制項A的間距。而layout_margin是A控制項所在的控制項為parent控制項,是A與其的間距。
其實概念很簡單,padding是站在父view的角度描述問題,它規定它裡面的內容必須與這個父view邊界的距離。margin則是站在自己的角度描述問題,規定自己和其他(上下左右)的view之間的距離,如果同一級只有一個view,那麼它的效果基本上就和padding一樣了
如
當按鈕分別設定以上兩個屬性時,得到的效果是不一樣的。android:paddingLeft="30px"按鈕上設定的內容(例片)離按鈕左邊邊界30個像素android:layout_marginLeft="30px"整個按鈕離左邊設定的內容30個像素
二、使用樣本
1、兩個都不加
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" > <TextView android:layout_width="100dip" android:layout_height="100dip" android:background="#000000" android:text="Hello" /></RelativeLayout>
效果:
2、只加padding
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding ="100dip" android:background="#ffffff" > <TextView android:layout_width="100dip" android:layout_height="100dip" android:background="#000000" android:text="Hello" /></RelativeLayout>
效果:
3、只加margin
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" > <TextView android:layout_width="100dip" android:layout_height="100dip" android:background="#000000" android:layout_margin="30dip" android:text="Hello" /></RelativeLayout>
效果:
三、注意說明
在LinearLayout、RelativeLayout、TableLayout中,這2個屬性都是設定都是有效
在FrameLayout中,android:layout_margin是無效的,因為FrameLayout裡面的元素都是從左上方開始繪製的
在AbsoluteLayout中,沒有android:layout_margin屬性
在此提醒,xml參數中含有layout的參數項目為定義的控制項相對於parent的關聯,沒有的一般為本身的定義,以上內容與此相符。又類似於gravity跟layout_gravity,帶layout的是相對於parent的大體位置,而不帶的是自身內部內容的大體位置。
林炳文Evankaka原創作品。轉載請註明出處http://blog.csdn.net/evankaka
Android中padding與layout_margin的區別與用法