android:padding和android:layout_margin的區別

來源:互聯網
上載者:User

標籤:

margin和padding是隔開元素中最常用的兩個屬性

◆Padding屬性:

Padding屬性用來描述元素的邊框和它的子項目之間插入多少空間,它分為上(padding-top)右(padding-right)下(padding-bottom)左(padding-left)和一個捷徑padding

Margin屬性:

Margin屬性用來描述元素的邊框和包含它的父元素的邊框之間插入多少空間,和padding屬性類似,它也分為上(margin-top)右(margin-right)下(margin-bottom)左(margin-left)和一個捷徑margin

Android:layout_margin就是設定view的上下左右邊框的額外空間

android:padding是設定內容相對view的邊框的距離

在LinearLayout、RelativeLayout、TableLayout中,這2個屬性都是設定都是有效

在FrameLayout中,android:layout_margin是無效的,因為FrameLayout裡面的元素都是從左上方開始繪製的

在AbsoluteLayout中,沒有android:layout_margin屬性

 

 android:layout_alignParentRight="true" 屬性是子控制項針對父容器的。 且父容器必須是RelativeLayout。

之前一直沒有搞懂Android:padding和android:layout_margin的區別,其實概念很簡單,padding是站在父view的角度描述問題,它規定它裡面的內容必須與這個父view邊界的距離。margin則是站在自己的角度描述問題,規定自己和其他(上下左右)的view之間的距離,如果同一級只有一個view,那麼它的效果基本上就和padding一樣了。例如我的XML layout代碼如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:paddingLeft="10dip"    android:paddingRight="10dip"    android:paddingTop="10dip"    android:paddingBottom="10dip"    ><TextView      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="#FF0000"    android:text="@string/hello"    android:paddingLeft="50dip"    android:paddingRight="50dip"    android:paddingTop="50dip"    android:paddingBottom="50dip"android:layout_marginBottom="10dip"    />    <TextView      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="#FF0000"    android:text="@string/hello"    android:paddingLeft="50dip"    android:paddingRight="50dip"    android:paddingTop="50dip"    android:paddingBottom="50dip" android:layout_marginBottom="10dip"    />    <TextView      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="#FF0000"    android:text="@string/hello"    android:paddingLeft="50dip"    android:paddingRight="50dip"    android:paddingTop="50dip"    android:paddingBottom="50dip"    android:layout_marginBottom="10dip"    />    <TextView      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="#FF0000"    android:text="@string/hello"    android:paddingLeft="50dip"    android:paddingRight="50dip"    android:paddingTop="50dip"    android:paddingBottom="50dip"    android:layout_marginBottom="10dip"    /></LinearLayout>

  

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:paddingLeft="10dip"    android:paddingRight="10dip"    android:paddingTop="10dip"    android:paddingBottom="10dip"    ><TextView      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="#FF0000"    android:text="@string/hello"    android:paddingLeft="50dip"    android:paddingRight="50dip"    android:paddingTop="50dip"    android:paddingBottom="50dip"android:layout_marginBottom="10dip"    />    <TextView      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="#FF0000"    android:text="@string/hello"    android:paddingLeft="50dip"    android:paddingRight="50dip"    android:paddingTop="50dip"    android:paddingBottom="50dip" android:layout_marginBottom="10dip"    />    <TextView      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="#FF0000"    android:text="@string/hello"    android:paddingLeft="50dip"    android:paddingRight="50dip"    android:paddingTop="50dip"    android:paddingBottom="50dip"    android:layout_marginBottom="10dip"    />    <TextView      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="#FF0000"    android:text="@string/hello"    android:paddingLeft="50dip"    android:paddingRight="50dip"    android:paddingTop="50dip"    android:paddingBottom="50dip"    android:layout_marginBottom="10dip"    /></LinearLayout>

  

 那麼我會得到如下的效果,圖上已經很明確的標出來區別咯。

 

android:padding和android:layout_margin的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.