2017-05-16 109 views
4

我學習如何使用ConstraintLayout。並有問題。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'). 
的可能是什麼問題

?也許有可能更好地編寫這段代碼?

但預覽作品:screen

回答

13

您嘗試設置基於元素定義之前的約束。因此,要解決您的問題,你有兩種不同的方式:

首先解決:更換你的RelativeLayout(@ + ID/reserved_layout_search)通過限制ID值:

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

解決方法二:聲明你的btnScanQRCode ,btnScanQRCode和tvReservedContInfo小部件放在XML文件的RelativeLayout之前。

+0

THX!你幫我。我不知道你可以聲明@ + id,因此 – abbath0767

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> 
+0

thx!我按照你的描述做了同樣的事情,只是提前一點 - 正常工作 – abbath0767

+0

很高興知道:) – FAT

相關問題