前面的文章

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. ZOJ 1602 Multiplication Puzzle(区间DP)题解

    题意:n个数字的串,每取出一个数字的代价为该数字和左右的乘积(1.n不能取),问最小代价 思路:dp[i][j]表示把i~j取到只剩 i.j 的最小代价. 代码: #include<set> ...

  2. CodeChef - ELHIDARR Find an element in hidden array(互动题)题解

    题意:有一串不递减的串,串中的任意元素都有k个,除了一个元素,他只有1 <= n < k-1个,你现在能向oj做出以下操作: 输出:1 pos,oj会返回pos位置的元素值 输出:2 va ...

  3. 最最简单的c语言函数汇编分析

    0x01 环境 xp+vc6.0 0x02 代码 int plus(int x, int y) { return 0; } 以下是vc6.0的反汇编窗口 1: int plus(int x, int ...

  4. C# winform程序防止前台卡死

    https://blog.csdn.net/Emiedon/article/details/51069193 在实际开发中,如果需要实时的显示后台处理的情况,我们可能要在前台用一些控件去显示 所以我们 ...

  5. LOJ 534 花团(线段树+dfs栈)

    题意 https://loj.ac/problem/534 思路 又是复杂度错误的一题,\(O(n^2\log n)\) 能过 \(15000\) . 虽然看起来强制在线,其实是一道假的在线题.首先按 ...

  6. String、StringBuffer 的使用 ,两个面试问题

    1>统计不同类型字符个数 public static void main(String[] args) { //案例:统计不同类型字符个数 String password = "abZ ...

  7. C++ getline判断空行

    C++中getline用于逐行读取字符, 格式 getline(字符串,字符数) 将该行“字符数”个的字符读入“字符串” 如何判断所读是否为空行呢? strlen(字符串)==0就是空行

  8. 在服务器端对sshd做白名单

    1.添加用户 #useradd aaa #passwd aaa -->输入密码:123456 添加3个用户,bbb和ccc与aaa添加一样 2.添加白名单 #vim /etc/ssd/sshd_ ...

  9. git pull 提示 There is no tracking information for the current branch

    在执行git pull的时候,提示当前branch没有跟踪信息: git pull There is no tracking information for the current branch. P ...

  10. 如何用R来定制个性化PPT

    ReporteRs包可以创建word,ppt,html文档.它可以格式化R的输出:如可编辑的矢量图,复杂的表格报告功能,企业模板文档的重用(.docx和.pptx).它是一个很好的自动化报告工具,并且 ...