ConstraintLayout使用chain链布局实现上图效果,三个控件紧挨着为一个整体,水平居中。
布局说明:
Guideline:一个默认不现实的基准线,这里为了确定三个控件在父布局垂直位置,可以用其他方式解决。
layout_constraintLeft_toLeftOf=“parent”:此控件的左边和父布局的左边对齐
layout_constraintRight_toLeftOf="@+id/tvChain2":此控件右边和tvChain2的左边对齐
app:layout_constraintTop_toBottomOf="@id/guideLine2":此控件顶部和guideline2底部对齐,其实就是在guideline下面了
3个TextView之间相互约束,然后左边的TextView通过父布局左边约束,右边的TextView通过父布局右边约束,这样就可以把他们之间建立一个Chain链,但是默认之间三个TextView是在水平方向均分,在通过layout_constraintHorizontal_chainStyle="packed"让他们之间紧挨着,来达到我们想要的效果。
<?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=".act.ConstraintActivity">
<android.support.constraint.Guideline
android:id="@+id/guideLine2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.8" />
<!-- chain链 layout_constraintHorizontal_chainStyle 类型 -->
<TextView
android:id="@+id/tvChain1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/gray"
android:padding="5dp"
android:text="chain1"
android:textColor="@color/white"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/tvChain2"
app:layout_constraintTop_toBottomOf="@id/guideLine2" />
<TextView
android:id="@+id/tvChain2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/black"
android:padding="5dp"
android:text="chain2"
android:textColor="@color/white"
app:layout_constraintLeft_toRightOf="@id/tvChain1"
app:layout_constraintRight_toLeftOf="@id/tvChain3"
app:layout_constraintTop_toBottomOf="@id/guideLine2" />
<TextView
android:id="@+id/tvChain3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/gray"
android:padding="5dp"
android:text="chain3"
android:textColor="@color/white"
app:layout_constraintLeft_toRightOf="@id/tvChain2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/guideLine2" />
</android.support.constraint.ConstraintLayout>
会报错:
AGPBI: {"kind":"error","text":"No resource found that matches the given name (at \u0027layout_constraintRight_toLeftOf\u0027 with value \u0027@id/tvChain3\u0027).","sources":[{"file":"/Users/chen.yingjie/Downloads/demo/TestApp/app/src/main/res/layout/activity_game.xml","position":{"startLine":133,"startColumn":45,"startOffset":5111,"endColumn":57,"endOffset":5123}}],"original":"","tool":"AAPT"}
/Users/chen.yingjie/Downloads/demo/TestApp/app/build/intermediates/res/merged/debug/layout/activity_game.xml:125: error: Error: No resource found that matches the given name (at 'layout_constraintRight_toLeftOf' with value '@id/tvChain3').
原因:tvChain1的倒数第二行(27)使用@id/和tvChain2的倒数第二行(39)使用@id/导致的;
原理:@id/和@+id/是有区别的,xml文件从上到下解析,
使用@+id/时,如果R文件里没有就会定义这个id;
使用@id/时,如果id在R文件没有值就报错。
tvChain1用到tvChain2时,tvChain2还没有加载,这时候tvChain1在R文件中找不到id为tvChain2的值,所以报错。
解决方法:如果上面的控件用到下面控件的id,要使用@+id/。
<TextView
android:id="@+id/tvChain1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/gray"
android:padding="5dp"
android:text="chain1"
android:textColor="@color/white"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/tvChain2"
app:layout_constraintTop_toBottomOf="@id/guideLine2" />
<TextView
android:id="@+id/tvChain2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/black"
android:padding="5dp"
android:text="chain2"
android:textColor="@color/white"
app:layout_constraintLeft_toRightOf="@id/tvChain1"
app:layout_constraintRight_toLeftOf="@+id/tvChain3"
app:layout_constraintTop_toBottomOf="@id/guideLine2" />
<TextView
android:id="@+id/tvChain3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/gray"
android:padding="5dp"
android:text="chain3"
android:textColor="@color/white"
app:layout_constraintLeft_toRightOf="@id/tvChain2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/guideLine2" />