为了账号安全,请及时绑定邮箱和手机立即绑定

Android零基础入门第27节:正确使用padding和margin

标签:
Android

前面两期我们学习了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代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

<?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代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

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

今天就先到这里,如果有问题欢迎留言一起探讨,也欢迎加入Android零基础入门技术讨论微信群,共同成长!

原文链接:http://www.apkbus.com/blog-205190-68542.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消