ConstraintLayout可以有效地解决布局嵌套过多的问题,它使用约束的方式来指定各个控件的位置和关系的,有点类似于RelativeLayout,但远比RelativeLayout要更强大,但是在使用过程中对于某些特殊的场景,使用ConstraintLayout还是比较会出现一些我们意想不到的坑的。下面就列举下在使用过程中出现的坑。
坑1
样式:

<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="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imgLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imgRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp10"
android:layout_marginRight="@dimen/dp10"
android:text="标题测试题目标题测试题目标题测试题目标题测试题目标题测试题目标题测试题目标题测试题目标题测试题目"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/imgLeft"
app:layout_constraintRight_toLeftOf="@+id/imgRight"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
当我们使用上面这种写法,发现实现的效果和我们想的不是一样的。

app:layout_constraintLeft_toRightOf="@+id/imgLeft" app:layout_constraintLeft_toRightOf="@+id/imgLeft"
的约束效果都失效了。
原因:发现当我们将
android:layout_width="wrap_content" 修改成 android:layout_width="0dp" 之后达到了我们需要的效果