CollapsingToolbarLayout中找不到app:layout_scrollflags的资源ID

35
这个问题的标题已经说了一切。我遇到了错误: 在 CollapsingToolbarLayout 中找不到 app:layout_scrollflags 的资源标识符。我使用 Eclipse 并导入了 design library 的 jar 文件。我能够在我的类中使用 design support 布局,所以应该是正确的。
这是我使用的代码片段:
<LinearLayout 
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:orientation="vertical"
android:background="@color/activityBg"
tools:context=".MainActivity"
>

<android.support.design.widget.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <include
            layout="@layout/toolbar"/>

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

示例:http://android-developers.blogspot.in/2015/05/android-design-support-library.html


顺便提一下,这需要在CoordinatorLayout内的AppBarLayout中。 - shkschneider
将其放入AppBarLayout后仍然无法正常工作。 - DennisVA
8个回答

29

仅导入设计库的jar文件是不够的。在jar文件只包含类文件时,您需要导入android-design-library项目资源。

按照以下步骤操作:

  1. 导入android-design-library项目到目录"sdk/extras/android/support/design/",并将其设置为库项目(如果它不是)。
  2. 将上述项目作为库项目导入到您的主项目中。

您必须这样做,因为xmlns:app="http://schemas.android.com/apk/res-auto"表示您需要来自库项目或当前项目的本地资源,在这种情况下,它表示您需要来自android-design-library库项目的资源。


在导入style.xml文件后,我在设计库中遇到了错误。 - Sushant
1
你能提供更多细节吗? - SilentKnight

28

试试这个

在应用级别添加 build.gradle

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

然后点击 Build -> Rebuild Project


在这种情况下,SDK级别23能否代替24使用? - Bhunnu Baba
是的,你应该将从 compile 'com.android.support:design:23..'改为 compile 'com.android.support:design:24.2.1'你不需要使用24.2.1版本。 你可以替换成新版本。 - A.G.THAMAYS
“compile”已被弃用,必须使用“api”(预编译库)。版本必须与您的编译器匹配。IDE同步将提供适当的辅助工具。 - mobibob

6

正如其他人所说,您肯定需要将Design Support Library依赖项添加到您的Android应用程序中。最简单的方法是在应用程序级别的gradle文件中添加以下内容:

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

需要注意以下几点-

  1. 这个支持库不能使用比 compileSdkVersion 更高的版本。因为我使用的是 compileSdkVersion 25,所以必须使用 compile 'com.android.support:design:25.3.1',而不是 compile 'com.android.support:design:24.2.1'
  2. 使用 design 库需要使用 theme.appcompat 或其子类。如果您想要在 activity 中添加 actionbar,则主题应该是 AppCompat。在我的情况下,我使用的是 android:Theme.Material,所以它失败了。将其更改为 Theme.AppCompat.Light.NoActionBar 对我有效。

4

Androidx 解决方案:

如果您正在使用AndroidX,上述解决方案将不起作用

您需要实现以下内容:

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

请注意,也许您需要执行 Invalidate Caches/Restart 才能使它起作用:

文件 > 使缓存失效并重新启动

更多信息请查看迁移到 AndroidX 页面


2

尝试将以下代码添加到XML文件中:

app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"

1

以下是我的做法

  1. 从 * 导入 AppCompat V7

C:\Productivity\android-sdks\extras\android\support\v7\appcompat

并在导入项目对话框中勾选 复制到工作区

  1. 将 apcompat 的目标 Android 构建版本设置为 Android 6.0,方法如下:

项目 -> 属性 -> Android

这样应该可以消除所有编译错误。

  1. 从以下位置导入 Design Lib

C:\Productivity\android-sdks\extras\android\support\design

按照第 1 和第 2 步所述的方法导入项目。

  1. 将导入的 design lib 标记为 Android lib,如果显示错误,请将 AppCompat V7 lib 添加到您的 design lib 的构建路径中,方法如下:

项目 -> 属性 -> Android。

  1. 最后将设计和appcompat添加到您的Android项目的构建路径中

    项目->属性->Android

使用Android 6.0清理并构建您的项目,现在所有编译错误都应该已经消失了。

恭喜,现在您可以在Eclipse中使用Material Design。希望它能对某些人有所帮助。


1
为此,您应该使用这种层次结构的布局。在Eclipse中,请确保将设计支持库作为引用项目包含在内。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbarlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true" >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp" >

            <ImageView
                android:id="@+id/ivProfileImage"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                app:layout_collapseMode="parallax"
                android:contentDescription="@null"
                android:fitsSystemWindows="true"
                android:minHeight="100dp"
                android:scaleType="fitXY" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbar"
        android:layout_gravity="fill_vertical"
        app:layout_anchorGravity="top|start"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >
    </android.support.v4.widget.NestedScrollView>

把整个东西放进 AppBarLayout 之后仍然不能正常工作。 - DennisVA
尝试将线性布局移除并放入协调布局中,将其他内容放入嵌套滚动视图中。我最近实现了这个功能,它运行良好。 - Gopal Singh Sirvi
你使用的是Eclipse还是Android Studio? - DennisVA
除了appcompat v4/v7,您是否添加了任何JAR文件以使设计支持库正常工作? - DennisVA
不要只使用SDK中的项目设计(设计支持库)作为参考项目。 - Gopal Singh Sirvi
让我们在聊天室里继续这个讨论 - Gopal Singh Sirvi

0

在应用级别的build.gradle中添加

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

版本应该与您的compileSdkVersion匹配。 我使用的是版本28。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接