为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
Android

上一期我们一起学习了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代码:

?

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

<?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代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

<?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零基础入门技术讨论微信群,共同成长!

原文链接:http://www.apkbus.com/blog-205190-68536.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消