【安卓基础】使用Guideline与约束辅助布局的平分空间设计
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与约束辅助布局的平分空间设计的更多相关文章
- ConstraintLayoutDemo【约束性布局知识梳理】【基于1.1.3】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 在较新版本的Android Studio中新建项目默认使用 ConstraintLayout进行布局的. ConstraintLay ...
- MySQL基础(三)——约束
MySQL基础(三)--约束 约束是在表上强制执行的数据校验规则,主要用于维护表中数据的完整性以及当数据之间有以来关系时,保护相关的数据不会被删除. 根据约束对列的限制,可以划分为:单列约束(只约束一 ...
- 前端总结·基础篇·CSS(一)布局
目录 这是<前端总结·基础篇·CSS>系列的第一篇,主要总结一下布局的基础知识. 一.显示(display) 1.1 盒模型(box-model) 1.2 行内元素(inline) &am ...
- 基础篇 - SQL 的约束
基础篇 - SQL 的约束 约束 一.实验简介 约束是一种限制,它通过对表的行或列的数据做出限制,来确保表的数据的完整性.唯一性.本节实验将在实践操作中熟悉 MySQL 中的几种约束. 二 ...
- RelativeLayout布局下实现控件平分空间
起源:使用惯LinearLayout的朋友都知道,若想实现对屏幕的等分,只需要设置Layout_weight的值即可. 可是在RelativeLayout布局下实现等分却不是那么容易. 下面就简单介绍 ...
- Atitit. 数据约束 校验 原理理论与 架构设计 理念模式java php c#.net js javascript mysql oracle
Atitit. 数据约束 校验 原理理论与 架构设计 理念模式java php c#.net js javascript mysql oracle 1. 主键1 2. uniq index2 3. ...
- Oracle—表、约束、索引、表空间、分区、序列、统计信息
表.约束.索引.表空间.分区.序列.统计信息 一.表及其操作 1.创建表 create table 表名 ( 字段名1 字段类型 默认值 是否为空 , 字段名2 字段类型 默认值 是否为空, 字段名3 ...
- 《手把手教你》系列基础篇(九十七)-java+ selenium自动化测试-框架设计篇-Selenium方法的二次封装和页面基类(详解教程)
1.简介 上一篇宏哥介绍了如何设计支持不同浏览器测试,宏哥的方法就是通过来切换配置文件设置的浏览器名称的值,来确定启动什么浏览器进行脚本测试.宏哥将这个叫做浏览器引擎类.这个类负责获取浏览器类型和启动 ...
- [安卓基础] 004.运行app
运行你的app 这篇课程会教你: 1.如何在设备上运行你的app. 2.如何在模拟器上运行你的app. 当然,在学习之前,你还需要知道: 1.如何使用设备. 2.如何使用模拟器. 3.管理你的项目. ...
随机推荐
- Lucas定理的运用及组合数奇偶性的判断
组合数奇偶性的判断 对于C(n,k),若n&k == k 则c(n,k)为奇数,否则为偶数. 最直观的方法就是计算一下,然后看它的奇偶性:但是这个时间以及数据范围上都不允许: 另外一种方法就是 ...
- windows下编译libevent(2.1.8)及使用
一:获取libevent github地址:https://github.com/libevent/libevent/releases 下载2.1.8稳定版 二:编译libevent 我是用的visu ...
- ARTS第七周打卡
Algorithm : 做一个 leetcode 的算法题 ////////////////////////////////////////////////////////////////////// ...
- Java语言资源国际化步骤
语言资源国际化步骤: 1. 定义资源文件(如:language),需要使用命令native2ascii命令进行转码:(native2ascii是jdk中的转码工具,在jdk的bin目录下) 2 ...
- Go语言学习笔记(7)——函数和方法
Go语言中同时有函数和方法! 函数: go程序必须要包含一个main函数.main函数不能有任何参数和返回值! 1. 定义方法示例: func max(num1, num2 int) int { // ...
- SAS学习笔记12 SAS数据清洗和加工
set语句纵向合并 我们把a1和b1进行合并,并区分是来自哪个数据集,会用到in=选项 in=a是产生临时变量a,由于它是a1的选项,所以a的值=1(来自a1)或者=0(不来自a1) in=b是产生临 ...
- gmpy安装使用方法
gmpy是一种C编码的Python扩展模块,提供对GMP(或MPIR)多精度算术库的访问.gmpy 1.17是1.x系列的最终版本,没有进一步的更新计划.所有进一步的开发都在2.x系列(也称为gmpy ...
- BFS练习
1. 给定$d,k$, 求最小的被$d$整除, 且各数位仅有$k$和$0$组成的数. $(1\le k\le 9,1\le n\le 1e6)$ 从高位到低位$BFS$, BFS求出字典序最小的方案. ...
- Nginx 路由重写
很多时候我们的真实路由是隐藏的,都经过重写后展现到前台,下面简单写两个我经常用到的几个: 一般在配置*.host(在http里面引入的server配置)的时候会用到每个不同网址的路由重写,每一个rew ...
- 轻松搭建CAS 5.x系列(8)-在CAS Server增加双因素认证(DUO版)
概述说明 为了让系统更加安全,很多登录会加入双因素认证.何为双因素,如果把登陆作为开一扇门的话,那就是在原来的锁上再加一把锁,第二锁用新的钥匙,这样安全系数就更加高了. CAS是通过账号名和密码来认证 ...