ConstraintLayout布局已经推出了很长一段时间,功能也是比较强大,能有效减少界面的视图层级嵌套,一定程度提升界面绘制效率。

在项目中,我也是最近才选择开始使用ConstraintLayout,之前一直用的是LinearLayout + FrameLayout进行复杂布局。

在使用ConstraintLayout的时候遇到了一个问题,需要在水平方向平分空间给三个视图,之前只是简单了解约束布局的使用,并没有真正去在实战中使用。在后面的查阅文档和实践中,总结了三等分水平空间可以有下面两种办法:

ConstraintLayout约束参数:

1. 首先按照约束规则将需要平分空间的视图建立相互约束链条,一定要相互约束(第一个视图右边依赖第二个视图左边,第二个视图左边依赖第一个视图右边,第二个视图右边依赖第三个视图左边......)

2. 为每个视图加上app:layout_constraintHorizontal_weight属性,设置为1即可水平平分空间(layout_constraintHorizontal_weight作用类似于LinearLayout的weight属性,可以不设置此属性,默认为1)

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".surface.impl.message.ActivityMessage">

    <FrameLayout
        android:id="@+id/ucContentArea"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/ucContentArea2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </FrameLayout>

    <FrameLayout
        android:id="@+id/ucContentArea2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@id/ucContentArea"
        app:layout_constraintRight_toLeftOf="@id/ucContentArea3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </FrameLayout>

    <FrameLayout
        android:id="@+id/ucContentArea3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@id/ucContentArea2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </FrameLayout>

</android.support.constraint.ConstraintLayout>

使用Guideline辅助布局的设计

1. 使用Guideline可以在界面上任何位置(支持精确位置,百分比位置)创建一条水平或者垂直的参考系,Guideline其实就是一个不绘制任何内容的View,子视图通过约束参数可以相对于Guideline进行位置布局。

2. Guideline只需要关注:android:orientation、app:layout_constraintGuide_percent、app:layout_constraintGuide_begin、app:layout_constraintGuide_end这四个参数。

3. orientation用于设置参考系是水平还是垂直的,percent参数用于使用百分比控制参考系的位置,begin和end参数用于使用精确值控制参考线的位置(优先级:percent > begin > end,即设置percent时,begin不会生效)

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".surface.impl.message.ActivityMessage">

    <ImageView
        android:id="@+id/uaActReturn"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:src="@mipmap/global_ic_return"
        android:tint="#3388FF"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="个人消息"
        android:textColor="#000000"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="@id/uaActReturn"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/uaActReturn" />

    <View
        android:id="@+id/uiBarDivider"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#E3E4E6"
        app:layout_constraintTop_toBottomOf="@id/uaActReturn" />

    <android.support.constraint.Guideline
        android:id="@+id/umVertical333"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.33" />

    <android.support.constraint.Guideline
        android:id="@+id/umVertical666"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.66" />

    <TextView
        android:id="@+id/uiTabPolice"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:gravity="center"
        android:text="普通"
        android:textSize="12sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/umVertical333"
        app:layout_constraintTop_toBottomOf="@id/uiBarDivider" />

    <TextView
        android:id="@+id/uiTabMaintain"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="紧急"
        android:textSize="12sp"
        app:layout_constraintBottom_toBottomOf="@id/uiTabPolice"
        app:layout_constraintLeft_toRightOf="@id/umVertical333"
        app:layout_constraintRight_toLeftOf="@id/umVertical666"
        app:layout_constraintTop_toTopOf="@id/uiTabPolice" />

    <TextView
        android:id="@+id/uiTabInstall"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="其他"
        android:textSize="12sp"
        app:layout_constraintBottom_toBottomOf="@id/uiTabPolice"
        app:layout_constraintLeft_toRightOf="@id/umVertical666"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/uiTabPolice" />

</android.support.constraint.ConstraintLayout>

【安卓基础】使用Guideline与约束辅助布局的平分空间设计的更多相关文章

  1. ConstraintLayoutDemo【约束性布局知识梳理】【基于1.1.3】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 在较新版本的Android Studio中新建项目默认使用 ConstraintLayout进行布局的. ConstraintLay ...

  2. MySQL基础(三)——约束

    MySQL基础(三)--约束 约束是在表上强制执行的数据校验规则,主要用于维护表中数据的完整性以及当数据之间有以来关系时,保护相关的数据不会被删除. 根据约束对列的限制,可以划分为:单列约束(只约束一 ...

  3. 前端总结·基础篇·CSS(一)布局

    目录 这是<前端总结·基础篇·CSS>系列的第一篇,主要总结一下布局的基础知识. 一.显示(display) 1.1 盒模型(box-model) 1.2 行内元素(inline) &am ...

  4. 基础篇 - SQL 的约束

    基础篇 - SQL 的约束       约束 一.实验简介 约束是一种限制,它通过对表的行或列的数据做出限制,来确保表的数据的完整性.唯一性.本节实验将在实践操作中熟悉 MySQL 中的几种约束. 二 ...

  5. RelativeLayout布局下实现控件平分空间

    起源:使用惯LinearLayout的朋友都知道,若想实现对屏幕的等分,只需要设置Layout_weight的值即可. 可是在RelativeLayout布局下实现等分却不是那么容易. 下面就简单介绍 ...

  6. Atitit. 数据约束 校验 原理理论与 架构设计 理念模式java php c#.net js javascript mysql oracle

    Atitit. 数据约束 校验 原理理论与 架构设计 理念模式java php c#.net js javascript mysql oracle 1. 主键1 2. uniq  index2 3.  ...

  7. Oracle—表、约束、索引、表空间、分区、序列、统计信息

    表.约束.索引.表空间.分区.序列.统计信息 一.表及其操作 1.创建表 create table 表名 ( 字段名1 字段类型 默认值 是否为空 , 字段名2 字段类型 默认值 是否为空, 字段名3 ...

  8. 《手把手教你》系列基础篇(九十七)-java+ selenium自动化测试-框架设计篇-Selenium方法的二次封装和页面基类(详解教程)

    1.简介 上一篇宏哥介绍了如何设计支持不同浏览器测试,宏哥的方法就是通过来切换配置文件设置的浏览器名称的值,来确定启动什么浏览器进行脚本测试.宏哥将这个叫做浏览器引擎类.这个类负责获取浏览器类型和启动 ...

  9. [安卓基础] 004.运行app

    运行你的app 这篇课程会教你: 1.如何在设备上运行你的app. 2.如何在模拟器上运行你的app. 当然,在学习之前,你还需要知道: 1.如何使用设备. 2.如何使用模拟器. 3.管理你的项目. ...

随机推荐

  1. (九)Javabean与Jsp(来自那些年的笔记)

    目录 JavaBean 在JSP中使用JavaBean 标签用法 带标签体的 JavaBean 标签 setProperty 标签 getProperty 标签 JSP开发模式 案列:使用 模式一 编 ...

  2. CSP-S初赛

    初赛都过了好几天了,现在才想起来写点关于初赛的博客也真是...... 我是福建人,是在福建的赛点参加的CSP-S组的初赛,能力其实很弱,估分只能60多一点点.真是害怕一不小心这篇博客就变成了我的退役博 ...

  3. SAS学习笔记14 利用SAS绘制地图(二)

    笔记9讲过利用SAS绘制地图,这次接着讲 用中国各地(不含港澳台)的平均湿度数据来绘制地图 在地图上标出地名 宏%maplabel有9个参数,依次为:地图文件名.包含区域名称的数据集文件.输出的注释数 ...

  4. uboot-的start.S详细注解及分析

    原文地址:uboot-的start.S详细注解及分析 作者:zhouyg11 大多数bootloader都分为stage1和stage2两部分,u-boot也不例外.依赖于CPU体系结构的代码(如设备 ...

  5. Python 的 Mixin 类(转)

    转1:https://www.cnblogs.com/aademeng/articles/7262520.html 转2:https://blog.csdn.net/u010377372/articl ...

  6. 解决django的后台管理界面添加中文内容乱码问题

    在使用django migrate功能时,默认数据库的字符集不是utf8. 是latin 1,然后在后台管理model时,不允许有中文字符插入 解决方案: 在使用migrate建库之前先把数据库建立起 ...

  7. Jenkins 2017年用过

    Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能. Jenkins功能包括: 1.持续的软件版本 ...

  8. kong网关命令(一)

    上次在虚拟机里安装kong网关后,因为版本(1.4)太高,目前Kong Dashboard无法支持, 后续发现Git上有个开源工具Kong admin ui,下载源码并部署到NGINX. 但是发现使用 ...

  9. grafana根据不同prometheus server统计数据

    场景:由于采集的数据量巨大,所以部署了多台prometheus server服务器.根据业务领域分片采集,减轻prometheus server单节点的压力. 问题:grafana如何同时显示多数据源 ...

  10. Linux学习笔记(十四)磁盘管理(二):格式化、挂载以及Swap分区

    一.格式化 第一种写法 mkfs.文件系统 [分区名称(设备文件路径)] 例如:对sdb硬盘的第一个分区以ext3文件系统进行格式化 第二种写法 mkfs -t 文件系统  [分区名称(设备文件路径) ...