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

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

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

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

此文章版权为微信公众号分享达人秀(ShareExpert)——鑫鱻所有,若转载请备注出处,特此声明!

往期总结分享:

Android零基础入门第1节:Android的前世今生

Android零基础入门第2节:Android 系统架构和应用组件那些事

Android零基础入门第3节:带你一起来聊一聊Android开发环境

Android零基础入门第4节:正确安装和配置JDK, 高富帅养成第一招

Android零基础入门第5节:善用ADT Bundle, 轻松邂逅女神

Android零基础入门第6节:配置优化SDK Manager, 正式约会女神

Android零基础入门第7节:搞定Android模拟器,开启甜蜜之旅

Android零基础入门第8节:HelloWorld,我的第一趟旅程出发点

Android零基础入门第9节:Android应用实战,不懂代码也可以开发

Android零基础入门第10节:开发IDE大升级,终于迎来了Android Studio

Android零基础入门第11节:简单几步带你飞,运行Android Studio工程

Android零基础入门第12节:熟悉Android Studio界面,开始装逼卖萌

Android零基础入门第13节:Android Studio配置优化,打造开发利器

Android零基础入门第14节:使用高速Genymotion,跨入火箭时代

Android零基础入门第15节:掌握Android Studio项目结构,扬帆起航

Android零基础入门第16节:Android用户界面开发概述

Android零基础入门第17节:TextView属性和方法大全

Android零基础入门第18节:EditText的属性和使用方法

Android零基础入门第19节:Button使用详解

Android零基础入门第20节:CheckBox和RadioButton使用大全

Android零基础入门第21节:ToggleButton和Switch使用大全

Android零基础入门第22节:ImageView的属性和方法大全

Android零基础入门第23节:ImageButton和ZoomButton使用大全

Android零基础入门第24节:自定义View简单使用,打造属于你的控件

Android零基础入门第25节:简单且最常用的LinearLayout线性布局

Android零基础入门第26节:两种对齐方式,layout_gravity和gravity大不同

最新文章

  1. SQL Server 2016五大优势挖掘企业用户数据价值
  2. html5学习笔记(2)
  3. 第一章 zookeeper基础概念
  4. 利用反射得到android存储路径
  5. MySQL中别名的使用
  6. C++ 必知必会: 条款1: 数据抽象
  7. hdu 4647 Another Graph Game
  8. DBMS_LOB
  9. 从零开始学android开发-通过WebService进行网络编程,使用工具类轻松实现
  10. hdu4283 You Are the One 区间DP
  11. iframe中的模态框遮罩父窗口原理
  12. 智能合约语言 Solidity 教程系列6 - 结构体与映射
  13. EOS 新增的 WebAssembly 解释器,是什么鬼?
  14. Flutter控制屏幕旋转
  15. UNIX口令破解机
  16. mysql报错:Cause: com.mysql.jdbc.PacketTooBigException
  17. js的 new Date()日期格式化显示以及js获取时间戳
  18. html 相对路径 问题
  19. NET设计模式 第二部分 行为型模式(18):观察者模式(Observer Pattern)
  20. 用wiershark抓dns数据包

热门文章

  1. 移动CMPP3.0接口
  2. 图片拉伸:IOS开发UIImage中stretchableImageWithLeftCapWidth
  3. sql数据库时间转换convert
  4. BZOJ 1003 ZJOI2006 物流运输trans 动态规划+SPFA
  5. C#中的并发编程知识二
  6. numpy 辨异(二) —— np.identity()/np.eye()
  7. 44个 Javascript 变态题解析——分分钟让你怀疑人生
  8. Python Tricks(二十二)—— small tricks
  9. Android framework召回(3)binder使用和IBinder BpRefbase IInterface INTERFACE 之间的关系
  10. 微服务实践之路--RPC