簡體   English   中英

在包中找不到屬性“layout_behavior”的資源標識符

[英]No resource identifier found for attribute 'layout_behavior' in package

在我嘗試向其中添加庫之前,我的應用程序運行良好。 添加庫后,Android Studio 給了我以下錯誤:

錯誤:(26)在包“inf..”中找不到屬性“layout_behavior”的資源標識符

這是我的 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"/>

我嘗試了以下方法:

  • 刪除了庫
  • 重置 Android Studio 和我的電腦
  • 從 git 恢復到我的代碼的先前版本。

但是,錯誤仍然存​​在。 我該如何解決?

該字符串資源在Material Design支持庫中定義。

由於您未使用Material Design支持庫中的CoordinatorLayout ,因此您應該能夠安全地刪除app:layout_behavior屬性。 它可能是從其他代碼中剪切和粘貼的。

注意:如果您實際使用的是CoordinatorLayout並希望協作滾動行為起作用,則需要將最新版本的Material Design Support庫的依賴項添加到Gradle構建文件中:

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

更新:請注意,對於最新版本的Gradle,不推薦使用compile配置以支持implementationapi配置,因此您的依賴關系可能如下所示:

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

這只是一個例子; 閱讀本文時,版本號可能已過期,因此請確保您的版本與您要使用的支持庫版本相匹配。

有關更多信息: gradle中的實現和編譯之間的區別是什么

萬一其他人來自Google並犯了同樣的錯誤,那就是layout_behaviOr ,而不是layout_behavioUr

注意 :版本現在已更改,因此請使用最新版本替換以下版本。

如果不需要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'

對於那些使用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"

對於那些使用androidX的人

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

你需要包括這個

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

app替換“ app:layout_behavior =”@ string / appbar_scrolling_view_behavior:layout_behavior =”android.support.design.widget.AppBarLayout $ ScrollingViewBehavior

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

但它仍然給我錯誤Unresolved class @string/appbar_scrolling_view_behavior

所以我發現只是無效的緩存/重啟

最快的方法是File→Invalidate caches / Restart ...→Just Restart。

我希望它對你有所幫助。

注意:在Android Studio v3.4中顯示app:layout_behavior="@string/appbar_scrolling_view_behavior"未解析的類@string/appbar_scrolling_view_behavior但在您運行應用程序時有效。

我有這個問題。 我用兩步解決了我的問題。 1-在Android sdk管理器的SDKTools中下載最新版本的AndroidSupportLibrary包和AndroidSupportRepository包(或將它們升級到最新版本)。 2-將build.gradle fiel的支持依賴項更改為

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.+'

如果你沒有添加android材料,那么首先在build.gradle文件的依賴項中添加這一行

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

然后使用此屬性

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

將compile com.android.support:design:23.0.1添加到build.gradle依賴項中。

AgentKnopf回答 “確保將正確的依賴項添加到模塊的build.gradle文件中”,然后我想補充一下,你還要確保模塊與當前版本相同,如下所示:compile'c​​om.android.support:design : 25.3.1.0如果你沒有使用編譯'com.android.support:design**:23.1.0**'

編譯'com.android.support:design:23.0.1'

這個解決方案適合我

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM