2

我正在尝试将我自己的自定义标题视图添加到 Google 的新视图NavigationView中,但出于某种原因,无论我做什么,Android Studio 都会给我一个警告include,并且RelativeLayout此处不允许。我试着做

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_drawer"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/nav_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#cccc"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"/>
</android.support.design.widget.NavigationView>

但只有RecyclerView出现,我不知道还能做什么。

我真的很感激一些帮助。谢谢!

4

2 回答 2

6

您可以NavigationView使用app:headerLayoutXML 属性向 a 添加标头:

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_drawer"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_drawer_header" />

或以编程方式通过inflateHeaderView()

于 2015-08-03T22:05:14.173 回答
0

下面演示了设计支持库中NavigationView的典型用法。如您所见,NavigationView 有一个名为headerLayout的属性来设置其标题视图。

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <!-- Content -->
   <FrameLayout
        android:id="@+id/content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

   <!-- Drawer -->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer"/>    
</android.support.v4.widget.DrawerLayout>
于 2015-08-03T22:05:46.137 回答