Android中padding與layout_margin的區別與用法

來源:互聯網
上載者:User

標籤: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的區別與用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.