NavigationView抛出错误:未找到包中属性"menu"的资源标识符

Psy*_*her 15 android drawerlayout material-design navigationview androiddesignsupport

我想在我的布局中添加NavigationView,如下所示:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">


    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />

        <android.support.design.widget.NavigationView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/drawer" />

    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

但问题是我在编译过程中遇到错误,如下所示:

C:\Users\IBM_ADMIN\Documents\Androidprojects\supporttest\app\src\main\res\layout\activity_main.xml
Error:(22) No resource identifier found for attribute 'menu' in package 'ranjithnair02.com.supporttest'
Error:Execution failed for task ':app:processDebugResources'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Users\IBM_ADMIN\Documents\android-studio\sdk\build-tools\22.0.1\aapt.exe'' finished with non-zero exit value 1
Information:BUILD FAILED
Run Code Online (Sandbox Code Playgroud)

我在menu/drawer.xml中添加了菜单项.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">

    <group android:checkableBehavior="single">

        <item
            android:id="@+id/navigation_item_1"
            android:checked="true"
            android:icon="@drawable/abc_tab_indicator_mtrl_alpha"
            android:title="First" />

        <item
            android:id="@+id/navigation_item_2"
            android:icon="@drawable/abc_btn_check_material"
            android:title="Second" />
      </group>
</menu>
Run Code Online (Sandbox Code Playgroud)

Pat*_*ick 45

确保您对Android设计支持库具有正确的依赖关系.选择错误的一个很容易 - 因为Google似乎发布了两个不同的依赖字符串:

在此答案时,Android开发人员博客(以及用户hungryghost的评论)具有正确的依赖关系字符串,而支持库主页上的依赖关系字符串不起作用.

使用这个: compile 'com.android.support:design:26.1.0'

请注意,版本是22. 2 .0(错误:22.0.0)并且该包被称为设计(错误:支持设计)

完成这些更改后,请使用Android Studio菜单"重建项目"或更改gradle文件后出现的"同步"按钮.这最终使它对我有用.


Din*_*nny 7

将以下行添加到应用gradle文件。

依赖项{.....

  • 实施'com.android.support:design:26.1.0'

....

}

对于gradle版本3.0.1