如何使用菜单布局将自定义项目添加到NavigationView? [英] How can I add a custom item to a NavigationView with a menu layout?

查看:92
本文介绍了如何使用菜单布局将自定义项目添加到NavigationView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试通过使用新的 NavigationView 类.如果您只想在文档的示例中向左移动图标,向右移动文本,那么效果很好,但是如果我想向布局中添加一个具有android.support.v7.widget.SwitchCompat的布局的单个自定义视图,该怎么办呢?电影应用(请参见下面的屏幕截图)?

I've been experimenting with simplifying some navigation drawer code by making use of the new NavigationView class in the Android design support library. It works great if you just want icons on the left, and text on the right like in the example in the documentation, but what if I want to add a single custom view to the layout which has an android.support.v7.widget.SwitchCompat like in the Google Play Movies app (see screenshot below)?

我尝试使用actionLayout属性来指定自定义布局文件,如下面的示例代码所示,但是由于该属性无效,因此该属性似乎被忽略了.

I've tried using the actionLayout attribute to specify a custom layout file like in the example code below, however that attribute appeared to be ignored as it didn't work.

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>

我该如何使用它?理想情况下,我想在仍然使用菜单布局的同时添加自定义视图,但是如果不使用粗俗的技巧(例如,点击由支持库生成的现有布局)是不可能的,那么我想知道代码量最少的解决方案,仍然值得过渡到NavigationView.

How can I get this to work? Ideally I'd like to add the custom view while still making use of my Menu layout, but if that's not possible without using a crude hack (like tapping into an existing layout generated by the support library) then I'd like to know the solution with the least amount of code, and which still makes it worth transitioning to NavigationView.

推荐答案

actionLayout属性是

NavigationView提供了一种构建导航抽屉的便捷方法,包括使用菜单XML文件创建菜单项的能力.我们已经扩展了功能,使其能够通过app:actionLayout或使用MenuItemCompat.setActionView()为项目设置自定义视图.

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().

因此问题中的代码现在应该可以正常工作.

So the code in the question should just work now.

这篇关于如何使用菜单布局将自定义项目添加到NavigationView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆