在包中找不到属性“barrierDirection”的资源标识符 - ConstraintLayout Barrier

Posted

技术标签:

【中文标题】在包中找不到属性“barrierDirection”的资源标识符 - ConstraintLayout Barrier【英文标题】:No resource identifier found for attribute 'barrierDirection' in package - ConstraintLayout Barrier 【发布时间】:2018-02-11 13:33:14 【问题描述】:

我正在尝试为托管在 GitHub (https://github.com/googlecodelabs/constraint-layout) 上的培训运行示例程序。

我更新了 build.gradle 以修复构建错误,但我无法编译和运行它。我得到了

在中找不到属性“barrierDirection”的资源标识符 包“com.google.googleio”

找不到资源标识符 包“com.google.googleio”中的属性“constraint_referenced_ids”

我使用的是 android Studio 2.3.3。

build.gradle(模块:app)

apply plugin: 'com.android.application'

android 
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig 
        applicationId "com.google.googleio"
        minSdkVersion 22
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    
    buildTypes 
        release 
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        
    


dependencies 
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'

activity_main_done.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:id="@+id/activity_main_done"
    android:layout_
    android:layout_
    app:layout_editor_absoluteX="0dp"
    app:layout_editor_absoluteY="80dp"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="80dp">

    <ImageView
        android:src="@drawable/singapore"
        android:layout_
        android:layout_
        app:layout_editor_absoluteX="0dp"
        app:layout_editor_absoluteY="0dp"
        android:id="@+id/header"
        android:scaleType="centerCrop"
        android:contentDescription="@string/placeholder"
        app:layout_constraintLeft_creator="1"
        app:layout_constraintTop_creator="1"
        app:layout_constraintRight_creator="1"
        app:layout_constraintBottom_creator="1"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp"
        app:layout_constraintLeft_toLeftOf="@+id/activity_main_done"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintTop_toTopOf="@+id/activity_main_done"
        tools:layout_constraintTop_creator="1"
        app:layout_constraintRight_toRightOf="@+id/activity_main_done"
        tools:layout_constraintRight_creator="1"
        app:layout_constraintBottom_toBottomOf="@+id/favorite"
        android:layout_marginBottom="16dp"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintVertical_bias="0.0" />

    <ImageView
        android:src="@drawable/ic_star"
        android:layout_
        android:layout_
        app:layout_editor_absoluteX="359dp"
        app:layout_editor_absoluteY="104dp"
        android:id="@+id/favorite"
        android:background="@drawable/info_background"
        android:padding="5dp"
        android:contentDescription="@string/placeholder"
        app:layout_constraintTop_creator="1"
        app:layout_constraintRight_creator="1"
        app:layout_constraintBottom_creator="0"
        tools:layout_editor_absoluteX="359dp"
        tools:layout_editor_absoluteY="104dp"
        app:layout_constraintTop_toTopOf="@+id/activity_main_done"
        tools:layout_constraintTop_creator="0"
        app:layout_constraintRight_toRightOf="@+id/activity_main_done"
        android:layout_marginEnd="16dp"
        tools:layout_constraintRight_creator="0"
        app:layout_constraintBottom_toBottomOf="@+id/activity_main_done"
        android:layout_marginBottom="16dp"
        tools:layout_constraintBottom_creator="0"
        app:layout_constraintVertical_bias="0.19" />

    <TextView
        android:text="@string/singapore"
        android:layout_
        android:layout_
        app:layout_editor_absoluteX="16dp"
        app:layout_editor_absoluteY="140dp"
        android:id="@+id/title"
        android:textSize="24sp"
        app:layout_constraintLeft_creator="1"
        app:layout_constraintTop_creator="0"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="140dp"
        app:layout_constraintLeft_toLeftOf="@+id/activity_main_done"
        android:layout_marginStart="16dp"
        tools:layout_constraintLeft_creator="0"
        app:layout_constraintTop_toBottomOf="@+id/header"
        android:layout_marginTop="16dp"
        tools:layout_constraintTop_creator="0" />

    <EditText
        android:layout_
        android:layout_
        android:inputType="textPersonName"
        android:text="@string/camera_value"
        android:ems="10"
        app:layout_editor_absoluteX="73dp"
        app:layout_editor_absoluteY="176dp"
        android:id="@+id/cameraType"
        app:layout_constraintLeft_creator="1"
        app:layout_constraintTop_creator="1"
        app:layout_constraintRight_creator="1"
        app:layout_constraintLeft_toLeftOf="@+id/settings"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintTop_toBottomOf="@+id/title"
        android:layout_marginTop="8dp"
        tools:layout_constraintTop_creator="0"
        app:layout_constraintRight_toRightOf="@+id/settings"
        tools:layout_constraintRight_creator="1"
        app:layout_constraintStart_toEndOf="@+id/labelBarrier"
        android:layout_marginStart="8dp" />

    <TextView
        android:text="@string/camera"
        android:layout_
        android:layout_
        app:layout_editor_absoluteX="16dp"
        app:layout_editor_absoluteY="189dp"
        android:id="@+id/cameraLabel"
        android:labelFor="@+id/cameraType"
        app:layout_constraintLeft_creator="1"
        app:layout_constraintBaseline_creator="1"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="189dp"
        app:layout_constraintLeft_toLeftOf="@+id/activity_main_done"
        android:layout_marginStart="16dp"
        tools:layout_constraintLeft_creator="0"
        app:layout_constraintBaseline_toBaselineOf="@+id/cameraType"
        tools:layout_constraintBaseline_creator="0" />

    <TextView
        android:text="@string/settings"
        android:layout_
        android:layout_
        app:layout_editor_absoluteX="16dp"
        app:layout_editor_absoluteY="238dp"
        android:id="@+id/settingsLabel"
        android:labelFor="@+id/settings"
        app:layout_constraintLeft_creator="1"
        app:layout_constraintBaseline_creator="1"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="238dp"
        app:layout_constraintLeft_toLeftOf="@+id/activity_main_done"
        android:layout_marginStart="16dp"
        tools:layout_constraintLeft_creator="0"
        app:layout_constraintBaseline_toBaselineOf="@+id/settings"
        tools:layout_constraintBaseline_creator="0" />

    <android.support.constraint.Barrier
        android:id="@+id/labelBarrier"
        android:layout_
        android:layout_
        app:barrierDirection="end"
        app:constraint_referenced_ids="cameraLabel,settingsLabel" />

    <EditText
        android:layout_
        android:layout_
        android:inputType="textPersonName"
        android:text="@string/camera_settings"
        android:ems="10"
        app:layout_editor_absoluteX="73dp"
        app:layout_editor_absoluteY="225dp"
        android:id="@+id/settings"
        app:layout_constraintLeft_creator="1"
        app:layout_constraintTop_creator="1"
        app:layout_constraintRight_creator="1"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintTop_toBottomOf="@+id/cameraType"
        android:layout_marginTop="8dp"
        tools:layout_constraintTop_creator="0"
        app:layout_constraintRight_toRightOf="@+id/description"
        tools:layout_constraintRight_creator="1"
        app:layout_constraintStart_toEndOf="@+id/labelBarrier"
        android:layout_marginStart="8dp" />

    <Button
        android:text="@string/upload"
        android:layout_
        android:layout_
        app:layout_editor_absoluteX="307dp"
        app:layout_editor_absoluteY="538dp"
        android:id="@+id/upload"
        app:layout_constraintRight_creator="1"
        app:layout_constraintBottom_creator="1"
        tools:layout_editor_absoluteX="307dp"
        tools:layout_editor_absoluteY="538dp"
        app:layout_constraintRight_toRightOf="@+id/activity_main_done"
        android:layout_marginEnd="16dp"
        tools:layout_constraintRight_creator="0"
        app:layout_constraintBottom_toBottomOf="@+id/activity_main_done"
        android:layout_marginBottom="16dp"
        tools:layout_constraintBottom_creator="0" />

    <Button
        android:text="@string/discard"
        android:layout_
        android:layout_
        app:layout_editor_absoluteX="211dp"
        app:layout_editor_absoluteY="538dp"
        android:id="@+id/discard"
        android:elevation="0dp"
        app:layout_constraintRight_creator="0"
        app:layout_constraintBaseline_creator="1"
        tools:layout_editor_absoluteX="211dp"
        tools:layout_editor_absoluteY="538dp"
        app:layout_constraintRight_toLeftOf="@+id/upload"
        android:layout_marginEnd="8dp"
        tools:layout_constraintRight_creator="0"
        app:layout_constraintBaseline_toBaselineOf="@+id/upload"
        tools:layout_constraintBaseline_creator="0" />

    <TextView
        android:text="@string/singapore_description"
        android:layout_
        android:layout_
        app:layout_editor_absoluteX="16dp"
        app:layout_editor_absoluteY="274dp"
        android:id="@+id/description"
        android:fadingEdge="vertical"
        android:ellipsize="end"
        android:textSize="15sp"
        app:layout_constraintLeft_creator="1"
        app:layout_constraintTop_creator="0"
        app:layout_constraintRight_creator="1"
        app:layout_constraintBottom_creator="0"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="274dp"
        app:layout_constraintLeft_toLeftOf="@+id/activity_main_done"
        android:layout_marginStart="16dp"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintTop_toBottomOf="@+id/settings"
        android:layout_marginTop="8dp"
        tools:layout_constraintTop_creator="1"
        app:layout_constraintRight_toRightOf="@+id/activity_main_done"
        android:layout_marginEnd="16dp"
        tools:layout_constraintRight_creator="1"
        app:layout_constraintBottom_toTopOf="@+id/discard"
        android:layout_marginBottom="8dp"
        tools:layout_constraintBottom_creator="1" />

</android.support.constraint.ConstraintLayout>

仅供参考 - 当我注释掉 android.support.constraint.Barrier 标记时,代码编译并运行。

【问题讨论】:

【参考方案1】:

Barrier 不存在于 constraint-layout 库的 1.0.2 中。它是1.1.0 的一部分,目前处于测试状态。您需要将依赖项切换回:

compile 'com.android.support.constraint:constraint-layout:1.1.0-beta1'

如果 Android Studio 抱怨它找不到这个库,请确保您的 项目级 build.gradle 文件看起来像 the one from the project,特别是在 allprojects 中配置了 Google 存储库:

allprojects 
    repositories 
        jcenter()
        maven 
            url 'https://maven.google.com'
        
    

【讨论】:

我更改为 1.0.2,因为由于找不到依赖项的错误,我无法编译它。出于某种原因,它今天在 1.1.0 中找到了它。谢谢! @ethan: AFAIK,1.1.0-beta1 不在本地 ConstraintLayout 存储库中,而是在 Google Maven 存储库中,这就是我的答案中的 sn-p 代码添加到项目中的内容。 maven url 'https://maven.google.com' 可以替换为 google()(至少在 Gradle 插件的 3.0.0 版本中,但仍处于测试阶段)。

以上是关于在包中找不到属性“barrierDirection”的资源标识符 - ConstraintLayout Barrier的主要内容,如果未能解决你的问题,请参考以下文章

“在包 'android' 中找不到属性 'showAsAction' 的资源标识符”

在包“com.app ....”中找不到属性“...”的资源标识符

错误:在包“android”中找不到属性“textAlignment”的资源标识符

错误:在包“android”中找不到属性“requestLegacyExternalStorage”和“preserveLegacyExternalStorage”的资源标识符

在包“cn.pedant.sweetalert”中找不到属性“ProgressIndeterminate”的资源标识符

错误:在包“com.google.example”main.xml 中找不到属性“adSize”的资源标识符