前面的文章

ConstraintLayout 介绍 (一)

ConstraintLayout约束属性 (二)

ConstraintLayout 宽高比和偏移量比(三)

此博文主要讲解:Guideline和Barrier


Guideline (了解,实际使用过程中我使用的不多)

当需要一个任意位置的锚点时,可以使用指示线(Guideline)来帮助定位,指示线实际上是 View 的子类,使用方式和普通的 View 相同,但指示线有着如下的特殊属性:

  • 宽度和高度均为0
  • 可见性为 View.GONE

案例:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!--重要的是Guideline是不会显示到界面上的,默认是GONE的。--> <android.support.constraint.Guideline
android:id="@+id/guidelineBegin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="100dp" /> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Button"
app:layout_constraintLeft_toLeftOf="@+id/guidelineBegin"
app:layout_constraintTop_toTopOf="parent" /> <android.support.constraint.Guideline
android:id="@+id/guidelineEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="100dp" /> <Button
android:id="@+id/buttonEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="Button2"
app:layout_constraintRight_toLeftOf="@+id/guidelineEnd"
app:layout_constraintTop_toTopOf="parent" /> <android.support.constraint.Guideline
android:id="@+id/guidelinePercent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.8" /> <Button
android:id="@+id/buttonPercent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="96dp"
android:text="Button3"
app:layout_constraintLeft_toLeftOf="@+id/guidelinePercent"
app:layout_constraintTop_toTopOf="parent" /> <!--当你要用一个控件占屏幕宽度的一半的时候,可以用layout_constraintGuide_percent --> </android.support.constraint.ConstraintLayout>

如图:(有三个指示线)

Barrier

直译为障碍、屏障。在约束布局中,可以使用属性constraint_referenced_ids属性来引用多个带约束的组件,

从而将它们看作一个整体,Barrier 的介入可以完成很多其他布局不能完成的功能;

它跟 Guideline  一样属于Virtual Helper objects,在运行时的界面上看不到,但是要比Guideline实用多了。

案例

<?xml version="1.0" encoding="utf-8"?>
<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"> <!--Barrier--> <!-- 原始方案: 当改变textView1 的字段内容时,textView3随之改变,
当改变textView2 的字段内容时,则失效
--> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="warehouse"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="hospita"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView1" /> <!--现在 Barrier 就已经定义好了,只剩下把textView3的约束从相对于 textView1 改为 相对于 Barrier 了:
app:layout_constraintLeft_toRightOf="@+id/textView1" ===> 不设置下面属性则会出现有的内容看不见 取消下面注释
app:layout_constraintLeft_toRightOf="@+id/barrier1"/
app:layout_constraintStart_toEndOf="@+id/barrier1" -->
<android.support.constraint.Barrier
android:id="@+id/barrier1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="end"
app:constraint_referenced_ids="textView1,textView2"
/> <TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginRight="10dp"
android:text="为了看到整体的效果,可以切换语言,此时你会看到Barrier会自动位于较宽的那个textView后面,也就间接让 textView3 也位于了正确的位置:"
app:layout_constraintLeft_toRightOf="@+id/barrier1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /> <!-- <android.support.constraint.Barrier
android:id="@+id/barrier7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="end"
app:constraint_referenced_ids="textView2,textView1" /> <TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="为了看到整体的效果,可以切换语言,此时你会看到 Barrier 会自动位于较宽的那个 textView 后面,也就间接让 textView3 也位于了正确的位置:"
app:layout_constraintStart_toEndOf="@+id/barrier7"
app:layout_constraintTop_toTopOf="parent" />--> </android.support.constraint.ConstraintLayout>

效果图:(你可以随便改变 textview1和textview2的长度,并不会影响textview3的显示)

很实用

参考地址:https://constraintlayout.com/basics/barriers.html

android -------- ConstraintLayout Guideline和Barrier(四)的更多相关文章

  1. android -------- ConstraintLayout Group和goneMargin(五)

    前面的文章 ConstraintLayout 介绍 (一) ConstraintLayout约束属性 (二) ConstraintLayout 宽高比和偏移量比(三) ConstraintLayout ...

  2. Android ConstraintLayout详解(from jianshu)

    Android ConstraintLayout详解 https://www.jianshu.com/p/a8b49ff64cd3 1. 概述     在本篇文章中,你会学习到有关Constraint ...

  3. Android系列之Fragment(四)----ListFragment的使用

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  4. Android异步更新UI的四种方式

    Android异步更新UI的四种方式 2015-09-06 09:23 segmentfault 字号:T | T 大家都知道由于性能要求,android要求只能在UI线程中更新UI,要想在其他线程中 ...

  5. 转--Android按钮单击事件的四种常用写法总结

    这篇文章主要介绍了Android按钮单击事件的四种常用写法总结,比较了常见的四种写法的优劣,有不错的参考借鉴价值,需要的朋友可以参考下     很多学习Android程序设计的人都会发现每个人对代码的 ...

  6. Android开发_Android数据的四种存储方式

    Android系统一共提供了四种数据存储方式.分别是:SharePreference.SQLite.Content Provider和File.由于Android系统中,数据基本都是私有的的,都是存放 ...

  7. Android 仿PhotoShop调色板应用(四) 不同区域颜色选择的颜色生成响应

    版权声明:本文为博主原创文章,未经博主允许不得转载.  Android 仿PhotoShop调色板应用(四) 不同区域颜色选择的颜色生成响应  上一篇讲过了主体界面的绘制,这里讲解调色板应用中的另外一 ...

  8. Android图表库MPAndroidChart(十四)——在ListView种使用相同的图表

    Android图表库MPAndroidChart(十四)--在ListView种使用相同的图表 各位好久不见,最近挺忙的,所有博客更新的比较少,这里今天说个比较简单的图表,那就是在ListView中使 ...

  9. Android艺术开发探索第四章——View的工作原理(下)

    Android艺术开发探索第四章--View的工作原理(下) 我们上篇BB了这么多,这篇就多多少少要来点实战了,上篇主席叫我多点自己的理解,那我就多点真诚,少点套路了,老司机,开车吧! 我们这一篇就扯 ...

随机推荐

  1. tp剩余未验证内容-4

    关于pop-up被blocked的问题 首先 这个pop-up的功能叫 popup blocker , 它是浏览器(包括ff, chrome等) 自身 所内置 的一个功能, 不是 安装的外部 插件/或 ...

  2. Win10 使用命令修复系统坏死点

    我的电脑win10系统升级多次以后,经常会出现所有设置有时点不开的情况 解决: C:\WINDOWS\system32>sfc /SCANNOW 开始系统扫描.此过程将需要一些时间. 开始系统扫 ...

  3. newcoder 筱玛的迷阵探险(搜索 + 01字典树)题解

    题目描述 筱玛是个快乐的男孩子. 寒假终于到了,筱玛决定请他的朋友们一起来玩迷阵探险. 迷阵可以看做一个n×nn×n的矩阵A,每个格子上有一个有一个数Ai,j. 入口在左上角的(1,1)处,出口在右下 ...

  4. FJUT3574 HOME_W的附加题(带权线段树)题解

    题意: 给定n个数a1,a2,a3,……an.和m次操作. 每次操作格式如下 x y k   表示将a[x]替换为y.并求替换后,前k小的数之和 思路:我们用带权线段树维护权值,这里就是维护i的个数n ...

  5. cmd使用管理员权限运行,启动路径不是当前目录

    https://stackoverflow.com/questions/672693/windows-batch-file-starting-directory-when-run-as-admin B ...

  6. js选择排序

    选择排序 平均时间复杂度O(n*n) 最好情况O(n*n) 最差情况O(n*n) 空间复杂度O(1) 稳定性:不稳定 function chooseSort (arr) { var temp; var ...

  7. x=x+1,x+=1,及x++的效率哪个最高,为什么?

    x=x+1: 1.     读取右X的地址 2.     X+1 3.     读取左X的地址 4.     将右值传给左边的X x+=1: 1.     读取右边的x的地址 2.     X+1 3 ...

  8. linux mysql操作命令大全

    1.linux下启动mysql的命令:mysqladmin start/ect/init.d/mysql start (前面为mysql的安装路径) 2.linux下重启mysql的命令:mysqla ...

  9. Mybatis工程搭建

    工程搭建 • 1依赖包 • 2配置文件 • 2.1spring-mybatis.xml • 2.2mybatis-config.xml自带配置文件 • 2.3 mapper(dao)对象 • 2.4 ...

  10. _faction

    一.自定义阵营独立于联盟,部落,联盟和部落玩家可以加入同一阵营 二._function_menu表可以配置自定义阵营开启 二.配合_pvp表,可以实现区域的自定义阵营PVP 三.配合_req表fact ...