繁体   English   中英

如何使用菜单布局将自定义项添加到NavigationView?

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

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

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>

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

Android支持库23.1现在支持 actionLayout属性:

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

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

NavigationViewFrameLayout的子类,它可以有多个子FrameLayout

但是,您可以使用android:layout_gravity属性将多个子项添加到FrameLayout并通过为每个子项分配重力来控制它们在FrameLayout中的位置。

这意味着您可以向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>

使用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中的keyPoint,实现处理程序,然后它将工作。

我遇到了同样的问题,发现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有关,不确定其他版本。)

您可能想看一下这个库: https//github.com/mikepenz/MaterialDrawer 它使导航抽屉易于使用,并且支持自定义视图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM