Android布局中Padding与Margin的区别

来源:互联网 发布:淘宝图片处理软件 编辑:程序博客网 时间:2024/04/29 04:24

盒模型

为了更加准确地控制TextView里面内容的位置,我们可以使用一系列的padding属性来控制。在使用padding属性之前,先科普一下padding和Marigin之间的区别,然后我们在通过实际的效果看看他们之间的差异。

下图所示是一个类似盒子的模型,我们将通过下面的模型来讲解Padding和Marigin之间的区别。从图中可以看出,在Container(父控件)里面有一个子控件,假设是一个TextView控件。其中Margin是子控件与父控件之间的间隔大小。Border是子控件的边框,它是子控件和父控件的边界。Padding是指子控件中的内容(Content Area)与子控件Border的间隔大小。

image

margin属性

Android中有一系列的margin属性,下面让我们看看其中的android:layout_marginRight属性,为了有一个对比的效果,我们先将marginRight设为0dip,再将其设为50dip,如以下两图所示

android:layout_marginRight="0dip"

android:layout_marginRight="50dip"

clip_image002[6]

image

从上图中,我们可以看出,左图TextView控件跟他的父控件的是没有右间隔的,而右图明显的有一块间隔(见右图黄色圈圈部分)。

marginRight相同的还有以下属性,它们的原理都相同,就不一一细讲了。

属性名相关方法描述android:layout_marginBottomsetMargins(int,int,int,int)Specifies extra space on the bottom side of this view.android:layout_marginEndsetMarginEnd(int)Specifies extra space on the end side of this view.android:layout_marginLeftsetMargins(int,int,int,int)Specifies extra space on the left side of this view.android:layout_marginRightsetMargins(int,int,int,int)Specifies extra space on the right side of this view.android:layout_marginStartsetMarginStart(int)Specifies extra space on the start side of this view.android:layout_marginTopsetMargins(int,int,int,int)Specifies extra space on the top side of this view.

 

padding属性

下面让我们来看看android:layout_paddingRight属性。我们将在以下布局中,通过改变android:layout_paddingRight属性,来看看变化。

image

为了有一个对比的效果,我们先将paddingRight设为0dip,再将其设为50dip,如以下两图所示

android:layout_paddingRight="0dip"android:layout_paddingRight="50dip"imageimage

从上图中,我们可以看出,左图TextView控件中的内容跟TextView的边框(border)是没有右间隔的,而右图明显的有一块间隔(见右图黄色圈圈部分)。

paddingRight相同的还有以下属性,它们的原理都相同,就不一一细讲了。

属性名相关方法描述android:paddingsetPaddingRelative(int,int,int,int)Sets the padding, in pixels, of all four edges.android:paddingBottomsetPaddingRelative(int,int,int,int)Sets the padding, in pixels, of the bottom edge; see padding.android:paddingEndsetPaddingRelative(int,int,int,int)Sets the padding, in pixels, of the end edge; see padding.android:paddingLeftsetPadding(int,int,int,int)Sets the padding, in pixels, of the left edge; see padding.android:paddingRightsetPadding(int,int,int,int)Sets the padding, in pixels, of the right edge; see padding.android:paddingStartsetPaddingRelative(int,int,int,int)Sets the padding, in pixels, of the start edge; see padding.android:paddingTopsetPaddingRelative(int,int,int,int)Sets the padding, in pixels, of the top edge; see padding.

0 0
原创粉丝点击