前面的文章

ConstraintLayout 介绍 (一)

ConstraintLayout约束属性 (二)

此博文主要讲解:

app:layout_constraintHorizontal_bias
app:layout_constraintDimensionRatio

1:app:layout_constraintDimensionRatio(宽高比/百分比布局)

这个属性感觉非常实用,按照比例来分配布局

案例:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"> <!--
layout_constraintDimensionRatio(宽高比)
如果想实现一个固定宽高比的顶部标题栏的话,可以将宽和高设置为 0dp,
然后为其设置 app:layout_constraintDimensionRatio 属性,设定宽高比为16:7-->
<TextView
android:id="@+id/tv1"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/colorAccent"
app:layout_constraintDimensionRatio="h,16:8"
android:gravity="center"
android:text="@string/app_name"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/> <!--底部 app:layout_constraintDimensionRatio="w,1:3"-->
<TextView
android:id="@+id/tv2"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/colorPrimary"
app:layout_constraintDimensionRatio="w,1:3"
android:gravity="center"
android:text="stringapp_name"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/> <!--剩余布局居中-->
<TextView
android:layout_width="100dp"
android:layout_height="0dp"
android:gravity="center"
android:text="@string/app_name"
android:background="#ff0000"
app:layout_constraintDimensionRatio="w,15:25"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv1"
app:layout_constraintBottom_toTopOf="@+id/tv2"
/> </android.support.constraint.ConstraintLayout>
app:layout_constraintDimensionRatio="h,16:8"  按照宽高的比例,也可以
app:layout_constraintDimensionRatio="w,1:3"   按照高宽的比例

效果图:


2:app:layout_constraintHorizontal_bias(偏移量比)

链条分为水平链条和竖直链条两种,分别用 layout_constraintHorizontal_chainStyle 和 layout_constraintVertical_chainStyle 两个属性来设置
属性值有以下三种:

  • spread
  • spread_inside
  • packed

默认值为 spread

可以通过下图来理解各个参数值的含义

举例(水平的)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"> <!--
layout_constraintHorizontal_chainStyle
app:layout_constraintHorizontal_bias(偏移量比)等
--> <!-- https://www.jianshu.com/p/b884b8c46584
spread:中间居中,且包含一段空格,平均间隔,4个空格
packed:中间居中,全部挨在一起
spread_inside: 左右贴边缘,中间居中,中间两个平分间隔
--> <TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#f5ec7e"
android:text="Hello World!"
android:gravity="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintHorizontal_weight="2"
app:layout_constraintRight_toLeftOf="@+id/tv2"
/> <TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#55e4f4"
android:gravity="center"
android:text="Hello World!"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toRightOf="@+id/tv1"
app:layout_constraintRight_toLeftOf="@+id/tv3"
app:layout_constraintTop_toTopOf="parent" /> <TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#f186ad"
android:gravity="center"
android:text="Hello World!"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toRightOf="@+id/tv2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /> <!--layout_constraintHorizontal_bias 不设置就居中
设置了则向水平偏移
app:layout_constraintVertical_bias="0.5" 垂直中间
-->
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/app_name"
android:background="@color/colorAccent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.2"
android:gravity="center"
/> </android.support.constraint.ConstraintLayout>

效果图:

  

垂直的:

<?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"> <!--垂直的比例布局 layout_constraintVertical_chainStyle--> <TextView
android:id="@+id/tv1"
android:layout_width="100dp"
android:layout_height="0dp"
android:background="#f5ec7e"
android:gravity="center"
android:text="@string/app_name"
app:layout_constraintBottom_toTopOf="@+id/tv2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread_inside"
app:layout_constraintVertical_weight="2" /> <TextView
android:id="@+id/tv2"
android:layout_width="100dp"
android:layout_height="0dp"
android:background="#55e4f4"
android:gravity="center"
android:text="Hello World!"
app:layout_constraintBottom_toTopOf="@+id/tv3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv1"
app:layout_constraintVertical_chainStyle="spread_inside"
app:layout_constraintVertical_weight="1" /> <TextView
android:id="@+id/tv3"
android:layout_width="100dp"
android:layout_height="0dp"
android:background="#f186ad"
android:gravity="center"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv2"
app:layout_constraintVertical_chainStyle="spread_inside"
app:layout_constraintVertical_weight="1" /> </android.support.constraint.ConstraintLayout>

效果图:

参考文档:

https://developer.android.google.cn/reference/android/support/constraint/ConstraintLayout#Chains

https://constraintlayout.com/basics/create_chains.html

https://www.jianshu.com/p/b884b8c46584

android -------- ConstraintLayout 宽高比和偏移量比(三)的更多相关文章

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

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

  2. android -------- ConstraintLayout Guideline和Barrier(四)

    前面的文章 ConstraintLayout 介绍 (一) ConstraintLayout约束属性 (二) ConstraintLayout 宽高比和偏移量比(三) 此博文主要讲解:Guidelin ...

  3. Android OpenGL ES(四)----调整屏幕的宽高比

    1.宽高比问题 我们如今相当熟悉这样一个事实,在OpenGL里,我们要渲染的一切物体都要映射到X轴和Y轴上[-1,1]的范围内,对于Z轴也一样.这个范围内的坐标被称为归一化设备坐标,其独立于屏幕实际尺 ...

  4. android ConstraintLayout布局

    解析ConstraintLayout的性能优势 Android新特性介绍,ConstraintLayout完全解析 1.子控件的位置约束属性: layout_constraintRight_toLef ...

  5. Android ConstraintLayout详解(from jianshu)

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

  6. c# winform DirectX播放器 可以任意设置宽高比 屏幕拉伸

    第一步:dll引用 Microsoft.DirectX.dll Microsoft.DirectX.AudioVideoPlayback.dll 如果没有的话,可能需要安装微软的DRECTX JDK ...

  7. 加载的过程中图片变形了? --教你自定义自动适配图片宽高比的RatioLayout

    很多同行在开发中可能会遇到这样的问题,就是在加载图片的时候会出现图片变形的问题.其实这很可能就是你的图片宽高比和图片所在容器的宽高比不匹配造成的.比如说图片的宽为200,高为100.宽高比就是2,那么 ...

  8. Android 之窗口小部件详解(三)  部分转载

    原文地址:http://blog.csdn.net/iefreer/article/details/4626274. (一) 应用程序窗口小部件App Widgets 应用程序窗口小部件(Widget ...

  9. CSS实现自适应下保持宽高比

    在项目中,我们可能经常使得自己设计的网页能自适应.特别是网站中的图片,经常要求在网页放大(或缩小)时,宽高同时放大(或缩小),而且不变形(即保持正常的长宽比).为了不变形,常用的方法就是设置width ...

随机推荐

  1. Spring 学习历程(二)

    JUnit测试 maven导入包 <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> &l ...

  2. 使用velocity 小技巧

    因为公司的需求,我使用了velocity模板进行文件生成.在这里先记录一下使用velocity模板时的一些小技巧: 1.截取字符串 注意,(1)需要使用.length()获取字符串长度:       ...

  3. Flutter基础用法解析

    解析开始 Flutter中一切皆widget,一切皆组件.学习Flutter中,必须首先了解Flutter的widget.先从最基本的MaterialApp和Scaffold开始了解 1 Materi ...

  4. C#开发者工具网

    使用key值[123456]对[50cms]进行对称加密-在线DES对称加密/解密- 开发者工具网  http://tool.sufeinet.com/Encrypt/DesEncrypt.aspx? ...

  5. Dart语言快速学习上手(新手上路)

    Dart语言快速学习上手(新手上路) // 声明返回值 int add(int a, int b) { return a + b; } // 不声明返回值 add2(int a, int b) { r ...

  6. js插入排序

    插入排序 平均时间复杂度O(n*n) 最差情况O(n*n) 最好情况O(n) 空间复杂度O(1) 稳定性:稳定 function insertSort (arr) { var len = arr.le ...

  7. P3211 [HNOI2011]XOR和路径

    思路 看到异或,容易联想到二进制位之间是相互独立的,所以可以把问题变成每个二进制位为1的概率再乘上(1<<pos)的值 假设现在考虑到pos位,设f[i]为第i个节点期望的异或和第pos位 ...

  8. Nginx配置示例

    server {listen 6080;server_name local.boheadmin; location / {proxy_pass http://127.0.0.1:8087;} loca ...

  9. python学习打卡 day07 set集合,深浅拷贝以及部分知识点补充

    本节的主要内容: 基础数据类型补充 set集合 深浅拷贝 主要内容: 一.基础数据类型补充 字符串: li = ["李嘉诚", "麻花藤", "⻩海峰 ...

  10. hdu 1115 Lifting the Stone 多边形的重心

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...