NavigationView 给出了一些错误

NavigationView gives some errors

我试图为我的应用程序创建一个 NavigationView,如下所示:

<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
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">

<!-- Toolbar instead of ActionBar so the drawer can slide on top -->
<android.support.v7.widget.Toolbar
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="@dimen/abc_action_bar_default_height_material"
  android:background="?attr/colorPrimary"
  android:minHeight="?attr/actionBarSize"
  android:theme="@style/AppTheme.Toolbar"/>

<!-- Real content goes here -->
<FrameLayout
  android:id="@+id/content"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1"/>

<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/background_dark"
app:headerLayout="@layout/drawer_header"
app:itemIconTint="@android:color/background_light"
app:itemTextColor="@android:color/background_dark"
app:menu="@menu/drawer"/>

</android.support.v4.widget.DrawerLayout>

但是在查找 headerLayout 时出现了几个错误(存在 drawer_header 布局,还有 drawer xml)。

它说:

No resource identifier found for attribute 'headerLayout' in package 'xxx.xxx'

两个XML都存在,所以我不知道我做错了什么...

我在 Eclpse 上使用 API 21 进行编译。

为什么会出现这个错误?

编辑: 拜托,我需要帮助...我仍然无法编译...我将我的代码上传到 Dropbox 给任何可能想要帮助我的人。 .. 仍然发现问题!

我尝试添加 appcompatv7 库,但它们给我错误...现在我的项目无法运行...请帮助!

Link to Dropbox here

您应该查看本教程以了解设计支持导航抽屉: http://antonioleiva.com/navigation-view/ 本教程最后还有 link 到 github 项目。

在查看了您的代码后,我知道您已完成此操作:

<DrawerLayout>
   <Toolbar/>
   <FrameLayout>your content here</FrameLayout>
   <NavigationView/>
</DrawerLayout>

但抽屉布局是这样工作的:

<DrawerLayout>
    <LinearLayout>
        <Toolbar/>
        <FrameLayout>your content here</FrameLayout>
    </LinearLayout>
    <NavigationView/>
</DrawerLayout>

简而言之 --> DrawerLayout 应该只有两个 children(here's the official doc 这么说)

这是我的例子。我将抽屉布局放在 CoordinatorLayout 中,因为我不想让抽屉挡住我的工具栏。所以你可以看到漂亮的汉堡包图标动画。

<android.support.design.widget.CoordinatorLayout 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"
    tools:context="<your package name.your activity name>">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

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

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />


        <android.support.design.widget.NavigationView
            android:id="@+id/navigation"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/drawer_header"
            app:itemBackground="@drawable/drawer_item_bg"
            app:itemIconTint="@color/drawer_item_text"
            app:itemTextColor="@color/drawer_item_text"
            app:menu="@menu/navigation_menu" />

    </android.support.v4.widget.DrawerLayout>

</android.support.design.widget.CoordinatorLayout>

那么您需要布局文件夹中名称为 drawer_header.xml 的另一个布局。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:customAttrs="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="156dp"
    android:background="@color/primary_color"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|start"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:text="@string/app_name"
        android:textColor="@color/primary_white"/>

</FrameLayout>

您的 drawable 文件夹中的一个名为 drawer_item_bg.xml

的 drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <color android:color="@color/primary_color_light"/>
    </item>
    <item>
        <color android:color="@android:color/transparent"/>
    </item>
</selector>

更不用说 style.xml

中的主题了
<style name="AppBase" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary_color</item>
    <item name="colorPrimaryDark">@color/primary_color_dark</item>
    <item name="colorAccent">@color/primary_yellow_bright</item>
    <item name="colorControlNormal">@color/primary_yellow</item>
    <item name="colorControlHighlight">@color/primary_color_light_100</item>
    <item name="colorControlActivated">@color/primary_yellow_bright</item>
    <item name="colorButtonNormal">@color/primary_yellow_bright</item>
    <item name="android:windowBackground">@color/primary_white</item>
    <item name="android:textColorPrimary">@color/primary_color</item>
    <item name="android:textColorSecondary">@color/primary_color_light</item>
    <item name="android:textColor">@color/primary_white</item>
    <item name="android:buttonStyle">@style/ButtonAppTheme</item>
</style>

最后在你的 build.gradle

中添加这两个依赖项
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'

升级到 Android Studio 后一切都解决了...

谢谢大家!