【问题标题】:Error with ConstraintLayout: No resource found that matches the given nameConstraintLayout 出错:找不到与给定名称匹配的资源
【发布时间】:2017-05-16 13:50:46
【问题描述】:

我学习了如何使用 ConstraintLayout。还有问题。

我的activity_test.xml

<?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"
    android:padding="@dimen/default_padding">

    <RelativeLayout
        android:id="@+id/reserved_layout_search"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@id/btnScanQRCode"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/btnScanQRCode"
        app:layout_constraintTop_toTopOf="parent">

        <EditText
            android:id="@+id/etSearchWorker"
            style="@style/EditTextSearch"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

        <Button
            android:id="@+id/btnClearSearch"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@drawable/ic_clear"/>

    </RelativeLayout>

    <Button
        android:id="@+id/btnScanQRCode"
        android:layout_width="@dimen/search_qr_button_size"
        android:layout_height="@dimen/search_qr_button_size"
        android:background="@drawable/button_qr_background"
        android:text="@string/button_qr_text"
        android:textColor="@color/white"
        app:layout_constraintLeft_toRightOf="@id/reserved_layout_search"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tvPlanningCountInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="ddsadsas"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/tvReservedContInfo"
        app:layout_constraintTop_toBottomOf="@id/btnScanQRCode"
        />

    <TextView
        android:id="@+id/tvReservedContInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dsdsadasdasad :44"
        app:layout_constraintLeft_toRightOf="@id/tvPlanningCountInfo"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/tvPlanningCountInfo"
        />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvReserved"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="@dimen/default_padding"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tvPlanningCountInfo"
        app:layout_constraintBottom_toBottomOf="parent"
        />

</android.support.constraint.ConstraintLayout>

当我构建/重建/清理项目时,我会处理错误:

/Users/ME/Documents/Projects/testApp/app/src/main/res/layout/activity_test.xml
Error:(14, 49) No resource found that matches the given name (at 'layout_constraintBottom_toBottomOf' with value '@id/btnScanQRCode').
Error:(16, 46) No resource found that matches the given name (at 'layout_constraintRight_toLeftOf' with value '@id/btnScanQRCode').
Error:(56, 46) No resource found that matches the given name (at 'layout_constraintRight_toLeftOf' with value '@id/tvReservedContInfo').

可能是什么问题?或许可以更优化地编写这段代码?

但预览有效:

【问题讨论】:

    标签: android android-constraintlayout


    【解决方案1】:

    您试图在定义元素之前基于元素设置约束。因此,要解决您的问题,您有两种不同的方法:

    第一个解决方案:将 RelativeLayout(@+id/reserved_layout_search) 约束 id 值替换为:

    @id/btnScanQRCode -> @+id/btnScanQRCode
    @id/btnScanQRCode -> @+id/btnScanQRCode
    @id/tvReservedContInfo -> @+id/tvReservedContInfo
    

    第二种解决方案:在 XML 文件中的 RelativeLayout 之前声明 btnScanQRCode、btnScanQRCode 和 tvReservedContInfo 小部件。

    【讨论】:

    • 谢谢!你帮我。我不知道你可以这样声明@+id
    • 非常感谢兄弟,我花了 2 天时间解决了您的解决方案的问题
    • @AmitDesale,我很高兴能帮助你,兄弟! ;)
    【解决方案2】:

    只需将 (@id/btnScanQRCode@id/tvReservedContInfo) 替换为 (@+id/btnScanQRCode@+id/tvReservedContInfo)。

    试试这个:

    <?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"
        android:padding="@dimen/default_padding">
    
        <RelativeLayout
            android:id="@+id/reserved_layout_search"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="@+id/btnScanQRCode"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/btnScanQRCode"
            app:layout_constraintTop_toTopOf="parent">
    
            <EditText
                android:id="@+id/etSearchWorker"
                style="@style/EditTextSearch"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                />
    
            <Button
                android:id="@+id/btnClearSearch"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:background="@drawable/ic_clear"/>
    
        </RelativeLayout>
    
        <Button
            android:id="@+id/btnScanQRCode"
            android:layout_width="@dimen/search_qr_button_size"
            android:layout_height="@dimen/search_qr_button_size"
            android:background="@drawable/button_qr_background"
            android:text="@string/button_qr_text"
            android:textColor="@color/white"
            app:layout_constraintLeft_toRightOf="@+id/reserved_layout_search"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
        <TextView
            android:id="@+id/tvPlanningCountInfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="ddsadsas"
            app:layout_constraintHorizontal_chainStyle="spread"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/tvReservedContInfo"
            app:layout_constraintTop_toBottomOf="@+id/btnScanQRCode"
            />
    
        <TextView
            android:id="@+id/tvReservedContInfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="dsdsadasdasad :44"
            app:layout_constraintLeft_toRightOf="@id/tvPlanningCountInfo"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="@id/tvPlanningCountInfo"
            />
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvReserved"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginTop="@dimen/default_padding"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvPlanningCountInfo"
            app:layout_constraintBottom_toBottomOf="parent"
            />
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • 谢谢!我做了和你描述的一样的,只是稍早一点 - 工作正常
    • 很高兴知道:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 2017-08-26
    相关资源
    最近更新 更多