如何将 header 添加到 Google 的新 NavigationView 中?
How do I add a header into Google's new NavigationView?
我正在尝试将自己的自定义 header 视图添加到 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
出现了,我不确定还能做什么。
非常感谢您的帮助。谢谢!
您可以使用 app:headerLayout
XML 属性将 headers 添加到 NavigationView
:
<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" />
编程
下面演示了设计支持库中 NavigationView 的典型用法。如您所见,NavigationView 有一个名为 headerLayout 的属性来设置其 header 视图。
<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>
我正在尝试将自己的自定义 header 视图添加到 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
出现了,我不确定还能做什么。
非常感谢您的帮助。谢谢!
您可以使用 app:headerLayout
XML 属性将 headers 添加到 NavigationView
:
<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" />
编程
下面演示了设计支持库中 NavigationView 的典型用法。如您所见,NavigationView 有一个名为 headerLayout 的属性来设置其 header 视图。
<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>