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. PAT甲级 并查集 相关题_C++题解

    并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...

  2. 某 游戏公司 php 面试题

    1.实现未知宽高元素的水平垂直居中,至少两种方法. <div, class="father"> <div class="son">< ...

  3. Python class and object

    # Python继承 class Person(object): """人""" def __init__(self, name, age) ...

  4. python学习-45 模块

    模块 -----模块包括三种: ····python标准库 ····第三方模块 ····应用程序自定义模块 -------应用程序自定义模块 1.建立两个py文件,一个是定义函数用的cal.py de ...

  5. Scratch 3.0,新在哪里?

    大家期待已久的Scratch 3.0,已经上线一段时间了. 学生们可轻松通过连接WeDo2.0和EV3机器人 进行scratch编程学习啦! 或许有些朋友还不太了解Scratch,没关系,小乐今天就为 ...

  6. odoo——日历的一对多与多对一

    # model文件 # -*- coding: utf-8 -*- from odoo import api, fields, models class TodoTestYear(models.Mod ...

  7. asp.net core-3.应用程序部署到iis

    asp.net core在部署到iis 上的时候,iis服务器要装一个AspNetCoreModule,网站—>模块  具体下载地址可以去网上搜索https://www.nuget.org/pa ...

  8. Mobile Phone Network CodeForces - 1023F (最小生成树)

    大意: 无向图, 其中k条边是你的, 边权待定, m条边是你对手的, 边权已知. 求如何设置边权能使最小生成树中, 你的边全被选到, 且你的边的边权和最大. 若有多棵最小生成树优先取你的边. 先将$k ...

  9. 音视频入门-03-RGB转成BMP图片

    * 音视频入门文章目录 * BMP 文件格式解析 BMP 文件由文件头.位图信息头.颜色信息和图形数据四部分组成. 位图文件头(14个字节) 位图信息头(40个字节) 颜色信息 图形数据 文件头与信息 ...

  10. 数据仓库之抽取数据:openrowset函数带bulk操作符的用法

    原文:数据仓库之抽取数据:openrowset函数带bulk操作符的用法 在做数据仓库时,最重要的就是ETL的开发,而在ETL开发中的第一步,就是要从原OLTP系统中抽取数据到过渡区中,再对这个过渡区 ...