Android零基础入门第27节:正确使用padding和margin
原文: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大不同
Android零基础入门第27节:正确使用padding和margin的更多相关文章
- Android零基础入门第29节:善用TableLayout表格布局,事半功倍
原文:Android零基础入门第29节:善用TableLayout表格布局,事半功倍 前面学习了线性布局和相对布局,线性布局虽然方便,但如果遇到控件需要排列整齐的情况就很难达到要求,用相对布局又比较麻 ...
- Android零基础入门第30节:两分钟掌握FrameLayout帧布局
原文:Android零基础入门第30节:两分钟掌握FrameLayout帧布局 前面学习了线性布局.相对布局.表格布局,那么本期来学习第四种布局--FrameLayout帧布局. 一.认识FrameL ...
- Android零基础入门第28节:轻松掌握RelativeLayout相对布局
原文:Android零基础入门第28节:轻松掌握RelativeLayout相对布局 在前面三期中我们对LinearLayout进行了详细的解析,LinearLayout也是我们用的比较多的一个布局. ...
- Android零基础入门第32节:新推出的GridLayout网格布局
原文:Android零基础入门第32节:新推出的GridLayout网格布局 本期主要学习的是网格布局是Android 4.0新增的布局,和前面所学的TableLayout表格布局 有点类似,不过他有 ...
- Android零基础入门第31节:几乎不用但要了解的AbsoluteLayout绝对布局
原文:Android零基础入门第31节:几乎不用但要了解的AbsoluteLayout绝对布局 前面几期基本学习了Android开发中常用的四种布局,之所以把AbsoluteLayout放在后面来学习 ...
- Android零基础入门第58节:数值选择器NumberPicker
原文:Android零基础入门第58节:数值选择器NumberPicker 上一期学习了日期选择器DatePicker和时间选择器TimePicker,是不是感觉非常简单,本期继续来学习数值选择器Nu ...
- Android零基础入门第59节:AnalogClock、DigitalClock和TextClock时钟组件
原文:Android零基础入门第59节:AnalogClock.DigitalClock和TextClock时钟组件 在前面一期,我们学习了DatePicker和TimePicker,在实际开发中其不 ...
- Android零基础入门第57节:日期选择器DatePicker和时间选择器TimePicker
原文:Android零基础入门第57节:日期选择器DatePicker和时间选择器TimePicker 在实际开发中,经常会遇见一些时间选择器.日期选择器.数字选择器等需求,那么从本期开始来学习And ...
- Android零基础入门第56节:翻转视图ViewFlipper打造引导页和轮播图
原文:Android零基础入门第56节:翻转视图ViewFlipper打造引导页和轮播图 前面两期学习了 ViewAnimator及其子类ViewSwitcher的使用,以及ViewSwitcher的 ...
随机推荐
- 嵌入式project师考试知识点总结 微内核结构
在操作系统的结构设计中,微内核结构表示的是 A) 总体式结构 B) 层次结构 C) 分布式结构 D) 客户机/server结构 微内核是指把操作系统结构中的内存管理.设备管理.文件系统等 ...
- MUI简介-最接近原生App体验的前端框架
MUI简介-最接近原生App体验的前端框架 一.总结 一句话总结:最接近原生App体验的前端框架 二.多端发布 – 开发一套代码,发布六个平台 真正彻底的跨平台开发,不是简单的跨iOS和Android ...
- ssh连接上腾讯云、华为云Linux服务器,一会就自动断开
客户端向服务端发送心跳 依赖 ssh 客户端定时发送心跳,putty.SecureCRT.XShell 都有这个功能. Linux / Unix 下,编辑 ssh 配置文件: # vim /etc/s ...
- 三步学会用spring开发OSGI——(第一步:环境篇)
Spring-DM是什么 Spring-DM 指的是Spring Dynamic Modules. dm Server 是一个完全模块化部署的,基于OSGi的Java服务器,为运行企业Java应用和S ...
- Linux中特别要注意用户与文件权限的问题
1.在使用Linux中,肯定会涉及不同用户的切换,但是如果不合理切换的话,会造成很多应用启动不了,所以这时候要多多使用ll看一下文件目录的权限问题,因为如果习惯用root启动程序,然后切换普通用户继续 ...
- KeePass v1.30
Changes from 1.29 to 1.30: New Features:Refined application icons (thanks to Victor Andreyenkov).Add ...
- htmlunit 模拟登录 数字验证码
使用htmlunit的好处有两点,相比httpclient,htmlunit是对浏览器的模拟,比如你定位一个按钮,就可以执行click()方法,此外不需要象在httpclient中一样编写复杂的代码, ...
- 一些常用的WebServices
天气预报Web服务,数据来源于中国气象局 Endpoint Disco WSDL IP地址来源搜索 WEB 服务(是目前最完整的IP地址数据) Endpoint Disco WSDL 随机英文 ...
- Android官方教程翻译(3)——创建一个简单的用户界面
转载请注明出处:http://blog.csdn.net/dawanganban/article/details/9839523 Building a Simple User Interface 创建 ...
- Qt常用函数 记录(update erase repaint 的区别)
一界面重载函数使用方法:1在头文件里定义函数protected: void paintEvent(QPaintEvent *event); 2 在CPP内直接重载void ----------::pa ...