约束布局是谷歌在
使用:
为什么要使用约束布局:
- ConstraintLayout还有一个优点,它可以有效地解决布局嵌套过多的问题。
- 实现的布局效果类似于相对布局但比相对布局性能更高。
盗个别人对比的图:
从图中我们可以看出性能上约束布局比相对布局性能要好上40%左右,因此有这么好的布局方式为什么不用呢。
使用方式:
1.在项目中添加依赖:
dependencies {
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha1'
}
2.布局中使用:
<?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">
<!--子控件-->
</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"
android:background="@color/black">
<!--垂直居中View-->
<View
android:id="@+id/view1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#00ff00"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!--水平居中的View-->
<View
android:id="@+id/view2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#ff0000"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
<!--完全居中的view-->
<View
android:id="@+id/view3"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!--右下角的View-->
<View
android:id="@+id/view4"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#0000ff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
<!--红色左侧部分居中及绿色上部分空白处居中-->
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#00ffff"
app:layout_constraintRight_toLeftOf="@id/view2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/view1"
/>
</android.support.constraint.ConstraintLayout>
效果图:
约束布局属性:
layout_constraintTop_toTopOf // 将所需视图的顶部与另一个视图的顶部对齐。
layout_constraintTop_toBottomOf // 将所需视图的顶部与另一个视图的底部对齐。
layout_constraintBottom_toTopOf // 将所需视图的底部与另一个视图的顶部对齐。
layout_constraintBottom_toBottomOf // 将所需视图的底部与另一个视图的底部对齐。
layout_constraintLeft_toTopOf // 将所需视图的左侧与另一个视图的顶部对齐。
layout_constraintLeft_toBottomOf // 将所需视图的左侧与另一个视图的底部对齐。
layout_constraintLeft_toLeftOf // 将所需视图的左边与另一个视图的左边对齐。
layout_constraintLeft_toRightOf // 将所需视图的左边与另一个视图的右边对齐。
layout_constraintRight_toTopOf // 将所需视图的右对齐到另一个视图的顶部。
layout_constraintRight_toBottomOf // 将所需视图的右对齐到另一个的底部。
layout_constraintRight_toLeftOf // 将所需视图的右边与另一个视图的左边对齐。
layout_constraintRight_toRightOf // 将所需视图的右边与另一个视图的右边对齐。
大概解释:
-
constraintRight/Left/Top/Bottom
布局的哪一边添加约束 -
toRight/Left/Top/BottomOf
约束到哪个View的右/左/上/下边
注意:在添加外边距时如果没有在对应方向上添加约束则可能没有效果,比如一个View在parent左侧,且添加了marginLeft=20dp,则需要设置layout_constraintLeft_toLeftOf或者layout_constraintLeft_toRightOf才能使外边距生效。