前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何正确使用padding和margin

如何正确使用padding和margin

作者头像
分享达人秀
发布2018-02-02 17:51:09
2.8K0
发布2018-02-02 17:51:09
举报
文章被收录于专栏:分享达人秀分享达人秀

前面两期我们学习了LinearLayout线性布局的方向、填充模型、权重和对齐,那么本期我们来学习LinearLayout线性布局的内边距和外边距。

关于padding和margin,很多同学傻傻分不清,相信通过今天的学习可以正确使用padding和margin。

一、内边距padding

默认情况下,组件相互之间是紧紧靠在一起的。但是有时候需要组件各边之间有一定的内边距,那就可以通过以下几个属性来设置,内边距的值是具体的尺寸,如5dp。

  • android:padding:为组件的四边设置相同的内边距。
  • android:paddingLeft:为组件的左边设置内边距。
  • android:paddingRight:为组件的右边设置内边距。
  • android:paddingTop:为组件的上边设置内边距。
  • android:paddingBottom:为组件的下边设置内边距。

内边距的原理如下图所示:

接下来通过一个简单的示例程序来学习android:padding的使用用法。

继续使用app/main/res/layout/目录下的activity_main.xml文件,在其中填充如下代码片段:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="padding"
        android:padding="20dp"
        android:background="#00ffff"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="normal"
        android:background="#caa926"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="padding"
        android:paddingLeft="50dp"
        android:paddingRight="50dp"
        android:paddingTop="50dp"
        android:paddingBottom="50dp"
        android:background="#00f05f"/>
</LinearLayout>

运行程序,可以看到下图所示界面效果:

二、外边距margin

通过设置内边距,只能设置内容相对于组件之间的距离,而组件之间仍然是相邻挨着的。在实际开发中,有时候需要组件之间有一定的间隔距离,那么就需要用到外边距了,可以通过以下几个属性来设置。

  • android:layout_margin:本组件离上下左右各组件的外边距。
  • android:layout_marginStart:本组件离开始的位置的外边距。
  • android:layout_marginEnd:本组件离结束位置的外边距。
  • android:layout_marginBottom:本组件离下部组件的外边距。
  • android:layout_marginTop:本组件离上部组件的外边距。
  • android:layout_marginLeft:本组件离左部组件的外边距。
  • android:layout_marginRight:本组件离右部组件的外边距。

外边距的原理如下图所示:

接下来通过一个简单的示例程序来学习android:layout_margin的使用用法。

将上面的示例程序的布局文件修改一下,如下所示:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="margin 20dp"
        android:layout_margin="20dp"
        android:background="#00ffff"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="normal"
        android:background="#caa926"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="margin 50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="50dp"
        android:background="#00f05f"/>
</LinearLayout>

重新运行程序,可以看到下图所示界面效果:

到此,关于LinearLayout线性布局的内边距和外边距已经学习完成,你都掌握了吗?padding和margin的区别是什么?

如果把布局的内边距和外边距放在一张图中比较会更加直观,如下图所示:

也有这种说法:margin代表的是偏移,padding代表的是填充。当然,你也可以根据自己的理解来总结。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-08-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 分享达人秀 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档