如何将自定义项目添加到具有菜单布局的 NavigationView?

How can I add a custom item to a NavigationView with a menu layout?

我一直在尝试通过使用 Android 设计支持库中的新 NavigationView class 来简化一些导航抽屉代码。如果您只想要左边的图标和右边的文本,就像文档中的示例一样,它会很好用,但是如果我想向布局添加一个自定义视图,它有一个 android.support.v7.widget.SwitchCompat 就像在Google Play 电影应用(见下面的屏幕截图)?

我试过使用 actionLayout 属性来指定自定义布局文件,就像下面的示例代码一样,但是该属性似乎被忽略了,因为它不起作用。

res/menu/navigation_drawer.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:id="@+id/group1"
            android:checkableBehavior="single">
        <item
            android:id="@+id/nav_screen1"
            android:icon="@drawable/ic_list_black_24dp"
            android:title="Screen 1" />
        <item
            android:id="@+id/nav_screen2"
            android:icon="@drawable/ic_search_black_24dp"
            android:title="Screen2"/>
    </group>
    <group  android:id="@+id/group2">
        <item
            android:id="@+id/nav_custom"
            android:icon="@drawable/custom_icon_24dp"
            app:actionLayout="@layout/nav_drawer_switch"
            android:title="Custom item with switch"/>
        <item
            android:id="@+id/nav_settings"
            android:icon="@drawable/ic_settings_black_24dp"
            android:title="Settings"/>
    </group>
</menu>

res/layout/nav_drawer_switch.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.SwitchCompat
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:text="Custom item with switch"/>
</LinearLayout>

我怎样才能让它工作?理想情况下,我想在仍然使用我的菜单布局的同时添加自定义视图,但如果不使用粗略的 hack(比如利用支持库生成的现有布局)是不可能的,那么我想知道代码量最少的解决方案,仍然值得过渡到 NavigationView.

NavigationViewFrameLayout的子类,可以有多个children:

You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

这意味着您可以将自定义视图添加到 NavigationView:

<android.support.design.widget.NavigationView
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/side_menu">

    <LinearLayout 
        android:orientation="horizontal" 
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_gravity="bottom">

        <android.support.v7.widget.SwitchCompat
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="night_mode"/>
    </LinearLayout>
</android.support.design.widget.NavigationView>

您可能想看看这个图书馆:https://github.com/mikepenz/MaterialDrawer。它使导航抽屉的使用变得容易,并且它支持自定义视图。

actionLayout 属性是 now supported in Android Support Library 23.1:

NavigationView provides a convenient way to build a navigation drawer, including the ability to creating menu items using a menu XML file. We’ve expanded the functionality possible with the ability to set custom views for items via app:actionLayout or using MenuItemCompat.setActionView().

所以问题中的代码现在应该可以正常工作了。

使用 switchcompat 创建一个布局并在菜单项中提到它

<item android:id="@+id/notification"
        android:title="Notification"
        app:actionLayout="@layout/notify" />`

然后通知布局添加这个

<android.support.v7.widget.SwitchCompat
    android:id="@+id/switch_compat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:checked="false"
    android:onClick="notify"/>

onclick 是关键点,在你的 Activity 中实现处理程序,然后它就可以工作了。

我遇到了同样的问题,发现 NavigationView 的行为有所不同,具体取决于:

  • 是否在 item 上设置 android:titleandroid:icon 属性
  • 您的操作布局包含哪种视图(例如 TextView 或其他)

未设置完全自定义项目的属性:

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
    tools:showIn="navigation_view">

    <item
        android:id="@+id/nav_custom"
        android:title="@null"
        app:actionLayout="@layout/nav_drawer_switch" />
</menu>

(请注意,您也可以不设置 android:title,但这会导致 Android Studio 中出现警告。)

结果:

(这是 com.android.support:design 的 27.1.1 版本,其他版本不确定。)