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

时间:2017-08-06 15:39:59   收藏:0   阅读:223

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

 

一、LinearLayout对齐

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

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

 

二、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线性布局

技术分享

技术分享

原文:http://www.cnblogs.com/cqkxzsxy/p/7294771.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!