android - 如何设置监听器在抽屉导航中切换?

标签 android xml

actvity_main_drawer.xml

<?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">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav1"
        android:icon="@drawable/ic_lock_outline_black_24dp"
        android:title="test1" />
    <item
        android:id="@+id/nav2"
        android:icon="@drawable/ic_add_a_photo_black_24dp"
        android:title="test2" />
    <item
        android:id="@+id/nav3"
        android:icon="@drawable/ic_face_black_24dp"
        android:title="test3" />

</group>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

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

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

    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

onoff_toggle.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:id="@+id/switch_layout"
android:layout_height="match_parent">

<android.support.v7.widget.SwitchCompat
    android:id="@+id/drawer_switch"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:checked="false"
    android:textOff="off"
    android:textOn="on"
    app:showText="true"/>

</LinearLayout>

这是我在抽屉导航菜单项 xml 代码中的开关

如何设置监听器切换

最佳答案

在准备导航 View 菜单项的地方尝试此操作。

NavigationView navigationView=(NavigationView)findViewById(id);

//get the menu from the navigation view
Menu menu=navigationView.getMenu();

//get switch view
SwitchCompat switchcompat=(SwitchCompat) MenuItemCompat.getActionView(menu.findItem(R.id.nav_notify)).findViewById(R.id.drawer_switch);

//add listener
switchcompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    //your action
                }
            });

关于android - 如何设置监听器在抽屉导航中切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37358766/

相关文章:

android - 如何启动 Vuforia AR 示例应用程序 android?

java - DataOutputStream 写入()不工作

java - Android 上的可点击 TableRow

xml - 为什么在 Google API 文档中通常建议使用 JSON 而不是 XML?

java - 从 xsd 到数据库。如何?

android - 有没有办法以 dpi 为单位获取屏幕尺寸

android - cordova emulate - 选择一个 Android 虚拟设备

python - 使用 Beautiful Soup 从 XML 返回嵌套属性

xml - JAXB(在 Jersey JAX-RS 中)是否有可能支持 java.util.HashMap 的 XML 和 JSON 格式

javascript - jquery加载函数在firefox中抛出 "XML Parsing Error: not well-formed"错误