前面的文章

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. Ajax详细剖析

    概述 对于WEB应用程序:用户浏览器发送请求,服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML)渲染并显示浏览器上. 传统的Web应用 一个简单操作需要重新 ...

  2. BottomNavigationBar 底部导航控件

    BottomNavigationBar 底部导航控件 属性 说明BottomNavigationBarItem 多个 item,iconSize icon大小currentIndex 默认选中第几个o ...

  3. (转)MongoDB学习

    (二期)25.分布式文件存储数据库MongoDB [课程25]mongod...命令.xmind96.9KB [课程25]MongoD...概念.xmind0.5MB [课程25]MongoDB简介. ...

  4. StringTie用法详解

    StringTie 参考链接: https://ccb.jhu.edu/software/stringtie/index.shtml?t=manual#input https://www.cnblog ...

  5. Tutorial: Implementation of Siamese Network on Caffe, Torch, Tensorflow

    Tutorial: Implementation of Siamese Network with Caffe, Theano, PyTorch, Tensorflow  Updated on 2018 ...

  6. (转) Supercharging Style Transfer

      Supercharging Style Transfer Wednesday, October 26, 2016 Posted by Vincent Dumoulin*, Jonathon Shl ...

  7. TIM定时器的应用

    TIM定时器的应用   ①输入捕获的应用: 上一节,我已阐述TIM的输入捕获具体作用有两个(如下图):     对输入信号的测量:                                    ...

  8. (转载)MySQl数据库-批量添加数据的两种方法

    方法一:使用excel表格 方法二:使用insert语句(FileWriter批量写入) 使用excel表格 1.打开数据表,按照表的字段在excel中添加数据.注意:表中字段名必须和excel中的名 ...

  9. A successful Git branching model——经典篇

    A successful Git branching model In this post I present the development model that I’ve introduced f ...

  10. Kubernetes工作流之Pods二

    Init Containers This feature has exited beta in 1.6. Init Containers can be specified in the PodSpec ...