使用来自Android设计支持库的NavigationView

5

我正在尝试按照这篇教程来使用来自Design Support Library的新DrawerLayout。

看起来Android Studio无法识别NavigationView布局。

这是我的main_activity布局:

<android.support.v4.widget.DrawerLayout
   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">

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:padding="20dp">
       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Hello"/>
   </LinearLayout>

<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_header"
    app:menu="@menu/drawer_menu"/>
</android.support.v4.widget.DrawerLayout>

当我尝试运行项目时,出现了以下错误:

Error: (19) No resource identifier found for attribute 'headerLayout'

但是我确实有这个drawer_header xml文件。


请问您能否添加活动和标题布局的代码? - N Jay
1个回答

17

我的错!

当然,你应该将android support design库导入到你的项目中。

因此,gradle(app)中的“dependencies”区域应该如下所示:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:design:22.2.0'
}

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