原文:Android零基础入门第26节:layout_gravity和gravity大不同

上一期我们一起学习了LinearLayout线性布局的方向、填充模型和权重,本期来一起学习LinearLayout线性布局的对齐。

一、LinearLayout对齐

gravity控制组件的重心,也叫对齐方式,表示view横向和纵向的停靠位置。主要通过以下两个属性来控制。

  • android:gravity:是对view组件本身来说的,是用来设置组件本身的内容应该显示在组件的什么位置,默认值是左侧。

  • android:layout_gravity:是相对于包含该元素的父元素来说的,设置该元素在父元素的什么位置。

其属性值主要有以下几种:

  • top:将对象放在其容器的顶部,不改变其大小。

  • bottom:将对象放在其容器的底部,不改变其大小。

  • left:将对象放在其容器的左侧,不改变其大小。

  • right:将对象放在其容器的右侧,不改变其大小。

  • center_vertical:将对象纵向居中,不改变其大小。垂直对齐方式:垂直方向上居中对齐。

  • fill_vertical:必要的时候增加对象的纵向大小,以完全充满其容器。垂直方向填充。

  • center_horizontal:将对象横向居中,不改变其大小。水平对齐方式:水平方向上居中对齐。

  • fill_horizontal:必要的时候增加对象的横向大小,以完全充满其容器。水平方向填充。

  • center:将对象横纵居中,不改变其大小。

  • fill:必要的时候增加对象的横纵向大小,以完全充满其容器。

  • clip_vertical:附加选项,用于按照容器的边来剪切对象的顶部和/或底部的内容。剪切基于其纵向对齐设置:顶部对齐时剪切底部;底部对齐时剪切顶部;除此之外剪切顶部和底部。垂直方向裁剪。

  • clip_horizontal:附加选项,用于按照容器的边来剪切对象的左侧和/或右侧的内容。剪切基于其横向对齐设置:左侧对齐时剪切右侧;右侧对齐时剪切左侧;除此之外剪切左侧和右侧。水平方向裁剪。

二、android:gravity

接下来通过一个简单的示例程序来学习android:gravity的使用用法。

继续使用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="match_parent"
android:layout_height="40dp"
android:text="top"
android:gravity="top"
android:textColor="#ffffff"
android:background="#ff0000" /> <TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="bottom"
android:gravity="bottom"
android:textColor="#ffffff"
android:background="#0000ff" /> <TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="left"
android:gravity="left"
android:textColor="#ffffff"
android:background="#ff0000" /> <TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="right"
android:gravity="right"
android:textColor="#ffffff"
android:background="#0000ff" /> <TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="center_vertical"
android:gravity="center_vertical"
android:textColor="#ffffff"
android:background="#ff0000" /> <TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="fill_vertical"
android:gravity="fill_vertical"
android:textColor="#ffffff"
android:background="#0000ff" /> <TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="center_horizontal"
android:gravity="center_horizontal"
android:textColor="#ffffff"
android:background="#ff0000" /> <TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="fill_horizontal"
android:gravity="fill_horizontal"
android:textColor="#ffffff"
android:background="#0000ff" /> <TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="center"
android:gravity="center"
android:textColor="#ffffff"
android:background="#ff0000" /> <TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="fill"
android:gravity="fill"
android:textColor="#ffffff"
android:background="#0000ff" /> <TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="clip_vertical"
android:gravity="clip_vertical"
android:textColor="#ffffff"
android:background="#ff0000" /> <TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="clip_horizontal"
android:gravity="clip_horizontal"
android:textColor="#ffffff"
android:background="#0000ff" />
</LinearLayout>

运行程序,可以看到下图所示界面效果:

三、android:layout_gravity

接下来通过一个简单的示例程序来学习android:layout_gravity的使用用法。

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

<?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">
<!-- 水平左右对齐 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ff0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="left"
android:background="#ffffff"
android:layout_gravity="left" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center_horizontal"
android:background="#ffffff"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="right"
android:background="#ffffff"
android:layout_gravity="right" />
</LinearLayout> <!-- 垂直上下对齐 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:background="#0000ff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="top"
android:background="#ffffff"
android:layout_gravity="top" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center_vertical"
android:background="#ffffff"
android:layout_gravity="center_vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bottom"
android:background="#ffffff"
android:layout_gravity="bottom" />
</LinearLayout> <!-- 整体居中对齐 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:background="#ff0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center"
android:background="#ffffff"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="vertical"
android:background="#0000ff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center"
android:background="#ffffff"
android:layout_gravity="center" />
</LinearLayout>
</LinearLayout>

重新运行程序,可以看到下图所示界面效果:

从上面两个示例可以发现android:layout_gravity和android:gravity两个属性的差别,一定要理解透彻。

今天就先到这里,如果有问题欢迎留言一起探讨,也欢迎加入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. Android零基础入门第10节:开发IDE大升级,终于迎来了Android Studio

    原文:Android零基础入门第10节:开发IDE大升级,终于迎来了Android Studio 通过前面几期的学习,我们知道了Android的前世今生,也了解了Android的系统架构和应用组件,也 ...

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

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

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

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

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

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

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

    原文:Android零基础入门第27节:正确使用padding和margin 前面两期我们学习了LinearLayout线性布局的方向.填充模型.权重和对齐,那么本期我们来学习LinearLayout ...

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

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

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

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

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

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

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

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

随机推荐

  1. [Django] Start a new django project

    Assume we already have a env created call 'demo-env': cd demo-env . bin/activate pip install django ...

  2. python 使用顺序表实现栈和队列

    栈: # -*- coding: utf-8 -*- # @author: Tele # @Time : 2019/04/24 下午 2:33 # 采用list(顺序表)实现栈结构,后入先出 clas ...

  3. Cocos2d-x 脚本语言Lua基本语法

    Cocos2d-x 脚本语言Lua基本语法 前面一篇博客对Lua这门小巧的语言进行了简单的介绍.本篇博客来给大家略微讲一下Lua的语法.不会长篇累牍得把Lua的全部语法都讲一遍,这里通过下面几点来讲L ...

  4. GANs(生成对抗网络)初步

    Image Completion with Deep Learning in TensorFlow 1. 基本思路 首先定义一个简单的.常见的概率分布,将其表示为 pz,不妨将其作为 [-1, 1] ...

  5. cxf maven 依赖 No message body writer foud.. contenttype:application/json

    最近使用cxf发布rest服务时遇到了如题的异常,最后发现是缺少依赖的问题.ps:我并没有使用spring cxf基本运行需要如下依赖 1 <dependency> <groupId ...

  6. 【17.69%】【codeforces 659F】Polycarp and Hay

    time limit per test4 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

  7. 实现在 .net 中使用 HttpClient 下载文件时显示进度

    在 .net framework 中,要实现下载文件并显示进度的话,最简单的做法是使用 WebClient 类.订阅 DownloadProgressChanged 事件就行了. 但是很可惜,WebC ...

  8. Formview单文档或对话框项目接受不到按键消息的解决办法

    当对话框或formview界面上有控件时,由于焦点在控件上,因此wm_char,wm_keydown等按键消息会被控件捕获,而导致对话框或formview无法接受该类按键消息.这时候通常的解决方法是在 ...

  9. twemproxy

    twemproxy架构分析——剖析twemproxy代码前编   twemproxy背景 在业务量剧增的今天,单台高速缓存服务器已经无法满足业务的需求, 而相较于大容量SSD数据存储方案,缓存具备速度 ...

  10. python 强制类型转换 以及 try expect

    强制类型转换: 字符串 -->  整型: 字符串 第一个  是 +  或者 -  ,会直接去掉 符号 ,返回 数字 如: a = '+123456' s = int(a) print(s) s ...