原文: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文件,在其中填充如下代码片段:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <TextView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="padding"
  10. android:padding="20dp"
  11. android:background="#00ffff"/>
  12.  
  13. <TextView
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="normal"
  17. android:background="#caa926"/>
  18.  
  19. <TextView
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="padding"
  23. android:paddingLeft="50dp"
  24. android:paddingRight="50dp"
  25. android:paddingTop="50dp"
  26. android:paddingBottom="50dp"
  27. android:background="#00f05f"/>
  28. </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的使用用法。

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <TextView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="margin 20dp"
  10. android:layout_margin="20dp"
  11. android:background="#00ffff"/>
  12.  
  13. <TextView
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="normal"
  17. android:background="#caa926"/>
  18.  
  19. <TextView
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="margin 50dp"
  23. android:layout_marginLeft="50dp"
  24. android:layout_marginRight="50dp"
  25. android:layout_marginTop="50dp"
  26. android:layout_marginBottom="50dp"
  27. android:background="#00f05f"/>
  28. </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大不同

Android零基础入门第27节:正确使用padding和margin的更多相关文章

  1. Android零基础入门第29节:善用TableLayout表格布局,事半功倍

    原文:Android零基础入门第29节:善用TableLayout表格布局,事半功倍 前面学习了线性布局和相对布局,线性布局虽然方便,但如果遇到控件需要排列整齐的情况就很难达到要求,用相对布局又比较麻 ...

  2. Android零基础入门第30节:两分钟掌握FrameLayout帧布局

    原文:Android零基础入门第30节:两分钟掌握FrameLayout帧布局 前面学习了线性布局.相对布局.表格布局,那么本期来学习第四种布局--FrameLayout帧布局. 一.认识FrameL ...

  3. Android零基础入门第28节:轻松掌握RelativeLayout相对布局

    原文:Android零基础入门第28节:轻松掌握RelativeLayout相对布局 在前面三期中我们对LinearLayout进行了详细的解析,LinearLayout也是我们用的比较多的一个布局. ...

  4. Android零基础入门第32节:新推出的GridLayout网格布局

    原文:Android零基础入门第32节:新推出的GridLayout网格布局 本期主要学习的是网格布局是Android 4.0新增的布局,和前面所学的TableLayout表格布局 有点类似,不过他有 ...

  5. Android零基础入门第31节:几乎不用但要了解的AbsoluteLayout绝对布局

    原文:Android零基础入门第31节:几乎不用但要了解的AbsoluteLayout绝对布局 前面几期基本学习了Android开发中常用的四种布局,之所以把AbsoluteLayout放在后面来学习 ...

  6. Android零基础入门第58节:数值选择器NumberPicker

    原文:Android零基础入门第58节:数值选择器NumberPicker 上一期学习了日期选择器DatePicker和时间选择器TimePicker,是不是感觉非常简单,本期继续来学习数值选择器Nu ...

  7. Android零基础入门第59节:AnalogClock、DigitalClock和TextClock时钟组件

    原文:Android零基础入门第59节:AnalogClock.DigitalClock和TextClock时钟组件 在前面一期,我们学习了DatePicker和TimePicker,在实际开发中其不 ...

  8. Android零基础入门第57节:日期选择器DatePicker和时间选择器TimePicker

    原文:Android零基础入门第57节:日期选择器DatePicker和时间选择器TimePicker 在实际开发中,经常会遇见一些时间选择器.日期选择器.数字选择器等需求,那么从本期开始来学习And ...

  9. Android零基础入门第56节:翻转视图ViewFlipper打造引导页和轮播图

    原文:Android零基础入门第56节:翻转视图ViewFlipper打造引导页和轮播图 前面两期学习了 ViewAnimator及其子类ViewSwitcher的使用,以及ViewSwitcher的 ...

随机推荐

  1. nopCommerce 3.9 接口笔记

    接口笔记 Nop.Services.Configuration ISettingService : 配置接口(查看) Nop.Services.Localization ILocalizationSe ...

  2. Net程序调试

    Net程序调试 前言 作为一个.net开发工程师,不管是在写桌面程序.服务程序或web程序,在开发阶段,我们必须非常熟悉vs的动态调试技能,当然web程序可能还需要调试前端的脚本或样式,这不在本文的讨 ...

  3. 如何在一个div中使其子div居中

    网上其他地方已讲述过对其的不同实现方式,今天主要做一个详细的汇总,希望对大家有帮助. ps:我面试的时候就被问到过这个问题,当时都回答错了,蓝瘦. 假设父div的类名为father,子div的类名为s ...

  4. 云平台DevOps实践

    基于TFS的.net技术路线的云平台DevOps实践   DevOps是近几年非常流行的系统研发管理模式,很多公司都或多或少在践行DevOps.那么,今天就说说特来电云平台在DevOps方面的实践吧. ...

  5. 【codeforces 765B】Code obfuscation

    [题目链接]:http://codeforces.com/contest/765/problem/B [题意] 让你把每个变量都依次替换成a,b,c,-.d这些字母; 且要按顺序先用a再用b-.c.d ...

  6. C++程序员经常问的11个问题

    下面的这些要点是对所有的C++程序员都适用的.我之所以说它们是最重要的,是因为这些要点中提到的是你通常在C++书中或网站上无法找到的.如:指向成员的指针,这是许多资料中都不愿提到的地方,也是经常出错的 ...

  7. C#连接Oracle数据库乱码问题

    C#连接Oracle数据库乱码问题 数据库连接之前,设置环境变量,如下 Environment.SetEnvironmentVariable("NLS_LANG", "A ...

  8. Android开发中如何加载API源码帮助开发

    在eclipse中添加android源码既可以帮助我们的开发,又能使我们边开发边学习. android环境的搭建:http://blog.csdn.net/dawanganban/article/de ...

  9. SWFTools PDF转换为SWF

    前言 在iText 制作PDF这篇博文中只是简单的介绍了如何制作PDF,为了能让PDF在Web页面中显示,我还需要通过SWFTools工具将PDF文件转换为SWF文件,然后通过SWF文件显示在Web网 ...

  10. c语言学习笔记(10)——结构体

    ------------------------------------------------------------------ # include <stdio.h> struct ...