因为实际在使用中经常会遇到还是很容易搞混,不知道该如何去选择边距的情况,所以单独把这个课题拎出来调查,实践了一番。

1. margin,指的是当前控件和父控件的边距。

举一个例子说明:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">     //第一层布局①背景全黑色,并且高和框都是match_parent
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#FFFFFF"     //第二层布局②设置为全白,并且设置和父控件的边距30dp
android:layout_marginLeft="30dp">
<EditText android:text="Hello World!" //文本框背景是黑色的,文字是白色的。
android:layout_width="match_parent"  //距离父控件(也就是第二层布局是60dp)
android:layout_height="wrap_content"
android:background="#000000"
android:layout_marginLeft="60dp"
android:textColor="#FFFFFF" />
</RelativeLayout>
</RelativeLayout>                 

2. padding,指的是当前控件的内边距,即控件中内容距离控件的边缘的距离。

同样举一个例子说明:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#000000">        //第一层布局①背景全黑色,并且高和框都是match_parent<EditText android:text="Hello World!"android:layout_width="wrap_content"       //文本框背景是白色的,文字是黑色的。android:layout_height="wrap_content"       //宽度随内容变化android:background="#FFFFFF"android:layout_marginLeft="60dp"android:paddingLeft="30dp"        //设置文字内容距离EditText的左边框是30dpandroid:paddingRight="60dp"       //设置文字内容距离EditText的右边框是60dpandroid:textColor="#000000" />
</RelativeLayout>

从例子上来看,EditText的宽度因为是随内容变化,所以实际宽度是距离左边框的30dp+“Hello World!"文字宽度+距离右边框60dp 三者合在一起的宽度。

3. 以上两个例子基本上可以理解了margin和padding的区别,但是这里引入了一个问题,如何设置两个控件之间的距离呢?
   这里我的理解是,对于不同的布局有不同的方法,说法很奇怪,还是以例子进行说明吧。

对于RelativeLayout来说,本来就是相对布局,所以它在控制控件和控件之间的位置有很多属性可以使用,常用的有以下:
3-1. 相对于父布局定位的属性(属性可以结合使用,分别实现左上角,右上角,左下角,右下角等方位)对应的值只有true和false
android:layout_alignParentLeft靠左
android:layout_alignParentTop靠上
android:layout_alignParentRight靠右
android:layout_alignParentBottom靠下
android:layout_centerInParent居中
3-2. 相对于控件定位的属性(属性可以结合使用,分别实现左上角,右上角,左下角,右下角等方位)因为是相对控件定位,所以对应的值是对应控件的id
android:layout_toLeftOf左边
android:layout_above上方
android:layout_toRightOf右边
android:layout_below下方
3-3. 其他一些属性
android:layout_alignLeft="@id/xxx"将控件的左边缘和给定ID控件的左边缘对齐
android:layout_alignRight="@id/xxx"将控件的右边缘和给定ID控件的右边缘对齐
android:layout_alignTop="@id/xxx"将控件的上边缘和给定ID控件的上边缘对齐
android:layout_alignBottom="@id/xxx"将控件的底边缘和给定ID控件的底边缘对齐
android:layout_toLeftOf="@id/xxx"将控件的右边缘和给定ID控件的左边缘对齐
android:layout_toRightOf="@id/xxx"将控件的左边缘和给定ID控件的右边缘对齐
android:layout_centerHorizontal="true"将控件置于水平方向的中心位置
android:layout_centerVertical="true"将控件置于垂直方向的中心位置
android:layout_alignParentLeft="true"将控件的左边缘和父控件的左边缘对齐
android:layout_alignParentTop="true"将控件的上边缘和父控件的上边缘对齐
android:layout_alignParentRight="true"将控件的右边缘和父控件的右边缘对齐
android:layout_alignParentBottom="true"将控件的底边缘和父控件的底边缘对齐

举一个例子说明:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#000000"><EditTextandroid:id="@+id/helloworld"android:text="Hello World!"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#FFFFFF"android:layout_marginLeft="30dp"android:textColor="#000000" /><EditText android:text="你好!"       //设置layout_toRightOfandroid:layout_width="wrap_content"      //让它显示在第一个EditText框的右边android:layout_height="wrap_content"   //然后结合margin设置边距android:layout_toRightOf="@id/helloworld"android:background="#FFFFFF"android:layout_marginLeft="60dp"android:textColor="#000000" />
</RelativeLayout>

=============================================================================

对于LinearLayout来说,控制两个控件之间的距离我认为还是使用margin。
因为LinearLayout本来就是线性排列,所以只需要设置margin就可以了。
例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#000000"><EditTextandroid:text="Hello World!"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#FFFFFF"android:layout_marginLeft="30dp"android:textColor="#000000" /><EditTextandroid:text="你好!"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#FFFFFF"android:layout_marginLeft="60dp"android:textColor="#000000" />
</LinearLayout>

注意一点:如果LinearLayout是水平排列的,使用的是marginLeft和marginRight。
        如果是垂直排列的,使用的是marginTop和marginBottom。
        如果只使用的是margin,那就是四个方向全部都设置了边距。

属性padding和margin的区别相关推荐

  1. 【css】padding 和 margin的区别

    css中有有一对经常是一起出现的属性:padding和margin,接下来我们以一个例子来看一下两个属性的作用及其区别. 实例演示: 使用属性前: 代码实现: <!DOCTYPE html> ...

  2. jsp+左间距_jsp 中padding 与margin的区别

    本文向大家简单介绍一下 Margin 和 Padding 属性中四个值的先后顺序及区别, Margin 和 Padding 属性中四个值的顺序为:上右下左,按照顺时针方向罗列的. AD: 你对 Mar ...

  3. html中padding和margin的区别和用法与存在的bug消除

    关于margin: 在需要border外侧添加距离时. 空白处不需要背景时. 相连的两个部分的地方需要加外边的边距时使用. 关于padding: 在需要border内侧添加距离时. 空白处需要背景时. ...

  4. android padding 和 margin的区别

    前面两期我们学习了LinearLayout线性布局的方向.填充模型.权重和对齐,那么本期我们来学习LinearLayout线性布局的内边距和外边距. 关于padding和margin,很多同学傻傻分不 ...

  5. 从零开始学_JavaScript_系列(六)——CSS的padding、margin、border属性超详细解释(图文)...

    结论请看最后的图片 关于定义: margin:层的边框以外留的空白 background-color:背景颜色 background-image:背景图片 padding:层的边框到层的内容之间的空白 ...

  6. 从零开始学_JavaScript_系列(六)——CSS的padding、margin、border属性超详细解释(图文)

    结论请看最后的图片 关于定义: margin:层的边框以外留的空白 background-color:背景颜色 background-image:背景图片 padding:层的边框到层的内容之间的空白 ...

  7. css属性之padding和margin

    css属性之padding和margin,并探究给行内元素设置内外边距是否有效! padding和margin的作用(重要) padding:内边距(影响盒子的大小) padding-top:上内边距 ...

  8. android:padding和android:margin的区别

    转载请说明博客地址:http://blog.csdn.net/qq_32059827/article/details/51487997 看了网上的类似博客,并没有给出确定的区别.现在具体分析一下pad ...

  9. android padding margin的区别,如何正确使用padding和margin

    原标题:如何正确使用padding和margin 前面两期我们学习了LinearLayout线性布局的方向.填充模型.权重和对齐,那么本期我们来学习LinearLayout线性布局的内边距和外边距. ...

最新文章

  1. android中getLocationInWindow 和 getLocationOnScreen的区别
  2. A.DongDong破密码
  3. 内核线程和用户线程(SMP)
  4. css3-5 css3鼠标、列表和尺寸样式怎么用(文字有关的样式会被继承)
  5. java容量_java中常用集合类的容量
  6. 转载——python字符串常用操作(加案例)
  7. AdvancedWindowsPasswordRecovery3 及 注册码
  8. iPhone模拟器截图
  9. python打印列表的下标和值的例子:
  10. wlop2020全奖励包_【图包】WLOP鬼刀壁纸【更新至2020年10月】
  11. c语言车队,狂野飙车8车队中c车的详细属性介绍
  12. 【机器学习】多分类学习的拆分策略
  13. 如何把自己的网站免费发布到互联网
  14. goahead实现文件下载功能实例详解
  15. CSS制作舞台聚光灯效果
  16. Elasticsearch 中的 Guice
  17. C#.net winform skin 皮肤大全devexpress,IrisSkin,DotNetSkin,SkinCrafter
  18. A股公司基本面数据API接口(JSON标准格式,Get请求方式)
  19. 北大AI公开课第九课--人工智能在视频中的应用by奇虎360颜水成
  20. 虚函数和纯虚函数--最通俗易懂的讲解

热门文章

  1. 新手如何做跨境电商?这七个经验干货请收好!
  2. iOS原生二维码扫描(一)
  3. kvmla openvz 评测
  4. Windows Overview
  5. Selenium WebDriver下载安装
  6. No appenders could be found for logger
  7. 小米嵌入式软件工程师笔试题目解析
  8. 外贸人如何使用intbell挖掘优质客户
  9. 大聪明教你学Java | 如何写出优雅的接口
  10. 车联网(智能网联汽车)无线电频率规划发布 助力制造强国和网络强国建设