android -------- 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
1、Bias 定义,水平偏移
和垂直偏移
注意前提 : 该属性生效的前提是 , 必须设置水平方向上的两个约束 ; Left
( 或 Start
) 约束
和 Right
( 或 End
) 约束都必须设置 ;
1、https://hanshuliang.blog.csdn.net/article/details/97102389?spm=1001.2101.3001.6650.5&utm
前言:有一个还在上大学的小学弟问博主有没有啥屏幕适配简单的方法,把鸿洋大神的帖子发给他,他说看不懂,鉴于其学Android的时间很短,所以自己出一篇比较容易的简单方法的帖子(百分比适配)。
博主准备的例子是csdn的启动页:效果图如下
现在用AS新建一个项目,都会默认用到一个布局–ConstraintLayout,而博主主要给大家介绍一下下图红框内的部分的前两种(类似于ps的参考线)
首先要会对位置进行设置–以前有写过一个博客——Android约束布局ConstraintLayout
点击Add
想必大家都对Google新贵ConstrainLayout的使用爱不释手。今天在使用app:layout_constraintHorizontal_bias属性的时候遇到点小疑问,研究了一会儿才知晓bias值的意义:即bias值=子View左相关的长度/(子View左相关的长度+其右相关的长度),默认值为0.5。在此记录并分享以下两种示例:
假设子view的宽度是父布局的1/2,父布局的宽度视作...
一、基本属性app:
layout_constraintLeft_toLeftOf(对齐约束)等
二、app:
layout_constraintHorizontal_bias(
偏移量比)等
三、app:
layout_constraintDimensionRatio(
宽高比)
四、app:
layout_constraintHorizontal_weight(类似Linear
Layout的wei...
转载自: http://blog.csdn.net/lmj623565791/article/details/78011599
一、概述 ConstraintLayout出现有一段时间了,不过一直没有特别去关注,也多多少少看了一些文字介绍,多数都是对使用可视化布局拖拽,个人对拖拽一直不看好,直到前段时间看到该文:
解析ConstraintLayout的性能优势
非常详尽的介绍了ConstraintL