前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >两种对齐方式,layout_gravity和gravity大不同

两种对齐方式,layout_gravity和gravity大不同

作者头像
分享达人秀
发布2018-02-02 17:52:50
2.7K0
发布2018-02-02 17:52:50
举报
文章被收录于专栏:分享达人秀分享达人秀

上一期我们一起学习了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文件,在其中填充如下代码片段:

代码语言:javascript
复制
<?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的使用用法。

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

代码语言:javascript
复制
<?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两个属性的差别,一定要理解透彻。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-08-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 分享达人秀 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档