Android布局中Padding与Margin的区别

盒模型

为了更加准确地控制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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值