在包中找不到属性 'layout_behavior' 的资源标识符

No resource identifier found for attribute 'layout_behavior' in package

在我尝试向其添加库之前,我的应用程序运行良好。添加库后,Android Studio 出现以下错误:

Error:(26) No resource identifier found for attribute 'layout_behavior' in package 'inf..'

这是我的 build.gradle 文件:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:support-v4:23.0.1'
    compile 'com.ogaclejapan.smarttablayout:utils-v4:1.3.0@aar'
    compile 'com.ogaclejapan.smarttablayout:library:1.3.0@aar'
    compile 'com.jpardogo.materialtabstrip:library:1.1.0'
    // compile 'com.lorentzos.swipecards:library:1.0.9@aar'
    compile 'com.android.support:cardview-v7:21.0.3'
    compile 'com.android.support:recyclerview-v7:21.0.3'
    compile 'com.melnykov:floatingactionbutton:1.3.0'
    compile project(':swipelib')
}

这是导致错误的 xml:

 <android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

我尝试了以下方法:

但是,错误仍然存​​在。我该如何解决?

该字符串资源在 Material 设计支持库中定义。

由于您没有使用 Material 设计支持库中的 CoordinatorLayout,您应该能够安全地删除 app:layout_behavior 属性。它可能是从其他代码中剪切和粘贴的。

注意:如果您实际使用 CoordinatorLayout 并希望协作滚动行为起作用,则需要将最新版本的 Material 设计支持库的依赖项添加到您的 Gradle 构建文件:

compile 'com.android.support:design:23.0.1'

更新:请注意,在最新版本的 Gradle 中,compile 配置已被弃用,取而代之的是 implementationapi 配置,因此您的依赖项可能看起来像这个:

implementation 'com.android.support:design:27.0.2'

这只是一个例子;当您阅读本文时,版本号可能已过时,因此请确保您的版本与您要使用的支持库的版本匹配。

更多信息:

注意:目前版本有变化,请将以下版本替换为最新版本。

如果不需要 layout_behavior,则接受的答案会消除错误,但是如果您确实想使用:

app:layout_behavior="@string/appbar_scrolling_view_behavior"

确保将正确的依赖项添加到模块的 build.gradle 文件中:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "21.1.2"

   //Other stuff....
}

dependencies {

    //Importing the design library fixes the build
    compile 'com.android.support:design:23.1.0'

    //Other libraries....
}

即将此行添加到您的依赖项:

compile 'com.android.support:design:23.1.0'

以防万一其他人来自 Google 并且犯了与我相同的错误,它是 layout_behaviOr,而不是 layout_behavioUr

添加编译com.android.support:design:23.0.1 进入您的 build.gradle 依赖项。

我有这个问题。我用两个步骤解决了我的问题。 1- 在 android sdk manager 的 SDKTools 中下载最新版本的 AndroidSupportLibrary 包和 AndroidSupportRepository 包(或将它们升级到最新版本)。 2- 将 support depenedencies ind build.gradle 字段更改为

compile 'com.android.support:appcompat-v7:25.+'
compile 'com.android.support:support-v4:25.+'
compile 'com.android.support:recyclerview-v7:25.+'
compile 'com.android.support:design:25.+'

"Make sure to add the proper dependency to the build.gradle file of your module",然后我要补充一点,你还要确保模块与你当前的版本相同:compile 'com.android.support:design:25.3.1.0 如果你没有使用 compile 'com.android.support:design**:23.1.0**'

对于那些使用 androidX 的人

com.android.support:design is now moved to com.google.android.material

你需要包括这个

implementation 'com.google.android.material:material:1.1.0-alpha02' 

我正在使用 Androidx,所以我实现了 implementation 'com.google.android.material:material:1.1.0-alpha05'

但它仍然给我错误 Unresolved class @string/appbar_scrolling_view_behavior

所以我发现只是使缓存无效/重新启动

最快的方法是文件 → 使缓存无效/重新启动... → 只是重新启动。

希望对你有所帮助

注意: 在 Android Studio v3.4 中显示 app:layout_behavior="@string/appbar_scrolling_view_behavior" 未解决 class @string/appbar_scrolling_view_behavior 但当您 运行 你的应用程序。

将“app:layout_behavior="@string/appbar_scrolling_view_behavior"替换为app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"

对于那些使用AndroidX并且不想添加旧库的人:

com.android.support:design:28.0.0

您可以改为添加:

implementation 'com.google.android.material:material:1.0.0'

并像这样使用它:

app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"

如果您还没有添加 android material 但首先在 build.gradle 文件

的依赖项中添加这一行
implementation 'com.google.android.material:material:1.0.0'

然后改用此属性

app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"