Padding 与Margin

来源:互联网 发布:linux ping 编辑:程序博客网 时间:2024/04/30 08:46

一、定义

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一样了

 

 

view sourceprint?
1.当按钮分别设置以上两个属性时,得到的效果是不一样的。
2.android:paddingLeft="30px"
3.按钮上设置的内容(例如图片)离按钮左边边界30个像素
4.android:layout_marginLeft="30px"
5.整个按钮离左边设置的内容30个像素


 

二、使用示例

1、两个都不加

 

view sourceprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03.android:layout_width="match_parent"
04.android:layout_height="match_parent"
05.android:background="#ffffff" >
06. 
07.<TextView
08.android:layout_width="100dip"
09.android:layout_height="100dip"
10.android:background="#000000"
11.android:text="Hello" />
12. 
13.</RelativeLayout>
效果:

 

2、只加padding

 

view sourceprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03.android:layout_width="match_parent"
04.android:layout_height="match_parent"
05.android:padding ="100dip"
06.android:background="#ffffff" >
07. 
08.<TextView
09.android:layout_width="100dip"
10.android:layout_height="100dip"
11.android:background="#000000"
12.android:text="Hello" />
13. 
14.</RelativeLayout>

 

效果:

3、只加margin

 

view sourceprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03.android:layout_width="match_parent"
04.android:layout_height="match_parent"
05.android:background="#ffffff" >
06. 
07.<TextView
08.android:layout_width="100dip"
09.android:layout_height="100dip"
10.android:background="#000000"
11.android:layout_margin="30dip"
12.android:text="Hello" />
13. 
14.</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
0 0
原创粉丝点击