先上图 , 该图是本博客的总结 ;
Bias 属性使用前提 :
app:layout_constraintHorizontal_bias
的前提 , 必须先设置水平方向的约束 , 使用 Left , Right 或者 Start , End 组合成的属性约束 , 如 ://将 被约束组件 的 左侧 约束到 目标组件 的左侧
app:layout_constraintLeft_toLeftOf
//将 被约束组件 的 左侧 约束到 目标组件 的右侧
app:layout_constraintLeft_toRightOf
//将 被约束组件 的 右侧 约束到 目标组件 的左侧
app:layout_constraintRight_toLeftOf
//将 被约束组件 的 右侧 约束到 目标组件 的右侧
app:layout_constraintRight_toRightOf
//将 被约束组件 的 开始 约束到 目标组件 的 结束
app:layout_constraintStart_toEndOf
//将 被约束组件 的 开始 约束到 目标组件 的 开始
app:layout_constraintStart_toStartOf
//将 被约束组件 的 结束 约束到 目标组件 的 开始
app:layout_constraintEnd_toStartOf
//将 被约束组件 的 结束 约束到 目标组件 的 结束
app:layout_constraintEnd_toEndOf
app:layout_constraintVertical_bias
的前提 , 必须先设置垂直方向的约束 , 使用 Top , Bottom 组合成的属性约束 , 如 ://将 被约束组件 的 上方 约束到 目标组件 的上方
layout_constraintTop_toTopOf
//将 被约束组件 的 上方 约束到 目标组件 的下方
layout_constraintTop_toBottomOf
//将 被约束组件 的 下方 约束到 目标组件 的上方
layout_constraintBottom_toTopOf
//将 被约束组件 的 下方 约束到 目标组件 的下方
layout_constraintBottom_toBottomOf
注意前提 : 该属性生效的前提是 , 必须设置水平方向上的两个约束 ; Left ( 或 Start ) 约束 和 Right ( 或 End ) 约束都必须设置 ;
水平偏移 ( app:layout_constraintHorizontal_bias ) :
与 组件左侧到左侧被约束位置的距离
与 组件右侧到右侧被约束的距离
之和 的 比例 ; 公式为 :
是组件左侧的缝隙 ,
是组件右侧的缝隙 ;
<?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"
tools:context=".MainActivity">
<!--
被 约束 的组件
组件宽高 占比 1/4
-->
<Button
android:id="@+id/button1"
android:layout_width="0dip"
android:layout_height="0dip"
android:text="被约束组件"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.25"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.25"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.5" />
</android.support.constraint.ConstraintLayout>
注意前提 : 该属性生效的前提是 , 必须设置垂直方向上的两个约束 ; Top 约束 和 Bottom 约束都必须设置 ;
垂直偏移 ( app:layout_constraintVertical_bias ) :
与 组件顶部到顶部被约束位置的距离
与 组件底部到底部被约束的距离
之和 的 比例 ; 公式为 :
是组件顶部的缝隙 ,
是组件底部的缝隙 ;
<?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"
tools:context=".MainActivity">
<!--
被 约束 的组件
组件宽高 占比 1/4
-->
<Button
android:id="@+id/button1"
android:layout_width="0dip"
android:layout_height="0dip"
android:text="被约束组件"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.25"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.25"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.25" />
</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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#FFFFFF">
<!--
被 约束 的组件
组件宽高 占比 1/4
-->
<TextView
android:id="@+id/button1"
android:layout_width="0dip"
android:layout_height="0dip"
android:text="被约束组件"
android:textColor="#000000"
android:gravity="center"
android:background="#00FF00"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.25"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.25"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.25" />
</android.support.constraint.ConstraintLayout>
:
:
博客写完了 , 画图画的眼有点花 来张图看看风景 ;
相关代码地址 :