約束佈局chain鏈:No resource found that matches the given name找不到id

在這裏插入圖片描述
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" />
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章