如何正确使用padding和margin

时间:2022-04-26
本文章向大家介绍如何正确使用padding和margin,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

前面两期我们学习了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文件,在其中填充如下代码片段:

<?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的使用用法。

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

<?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代表的是填充。当然,你也可以根据自己的理解来总结。