简体   繁体   English

在 Android 上使用设计支持库切换导航抽屉项目

[英]Switch in Navigation drawer item with Design Support Library on Android

I need to put Switch inside item in navigation drawer.我需要将 Switch 放在导航抽屉中的项目内。 I'm using new design support library, but I cannot find if it is posibble at all.我正在使用新的设计支持库,但我根本找不到它是否可行。 When using使用时

android:checkable

item is just full selected and that is not what I wish.项目只是完全选择,这不是我想要的。

This is screenshot of what I really want.这是我真正想要的截图。 Is that possible to achieve that?有可能实现吗?

在此处输入图像描述

Your menu item for the navigation drawer:您的导航抽屉菜单项:

<item
    android:id="@+id/nav_item1"
    android:icon="@drawable/ic_item1"
    android:title="item1"
    app:actionLayout="@layout/layout_switch"
    />

and the layout for that item:以及该项目的布局:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/drawer_switch"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:text=""/>

</LinearLayout>

EDIT:编辑:

I ended up using a different approach.我最终使用了不同的方法。 In fact, I found out that you can use any view in the drawer, so there's no point in bothering with the menu stuff.事实上,我发现你可以使用抽屉中的任何视图,所以没有必要为菜单内容烦恼。 Just create a view the usual way (with listeners, etc.) and add in to the drawer.只需以通常的方式(使用侦听器等)创建一个视图并将其添加到抽屉中。

One way I have found of doing this would be to use setActionView on the menuItem you want:我发现这样做的一种方法是在您想要的 menuItem 上使用 setActionView :

mNavigationView.getMenu().findItem(R.id.nav_connect)
        .setActionView(new Switch(this));

// To set whether switch is on/off use:
((Switch) mNavigationView.getMenu().findItem(R.id.nav_connect).getActionView()).setChecked(true);

Probably want a click listener as well to change the state of the Switch:可能还需要单击侦听器来更改 Switch 的状态:

mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {

        switch (menuItem.getItemId()) {
            case R.id.nav_connect:
                ((Switch) menuItem.getActionView()).toggle();
                return true;
        }
    }
}

I haven't tried, but you could probably use android:actionLayout="@layout/switch_layout" in xml and point to a custom layout you created.我还没有尝试过,但是您可能可以在 xml 中使用 android:actionLayout="@layout/switch_layout" 并指向您创建的自定义布局。

Also could try using an ActionProvider which might offer a little more robustness.也可以尝试使用 ActionProvider,它可能会提供更多的健壮性。 I haven't tried this method either though.不过这个方法我也没试过。

None of the answers seems to be complete so after some more research here is what I came up with:似乎没有一个答案是完整的,所以经过更多的研究后,我得出了以下结论:

drawer_switch.xml: drawer_switch.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SwitchCompat 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_switch"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" />

drawer_menu.xml: drawer_menu.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">

    <item android:title="@string/header">
        <menu>
            <item
                android:id="@+id/switch_item"
                android:icon="@drawable/ic_switch_item"
                app:actionLayout="@layout/drawer_switch"
                android:title="@string/switch_item" />
        </menu>
    </item>
</menu>

DrawerActivity.java: DrawerActivity.java:

SwitchCompat drawerSwitch = (SwitchCompat) navigationView.getMenu().findItem(R.id.switch_item).getActionView();
drawerSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // do stuff
                } else {
                    // do other stuff
                }
            }
});

DrawerActivity.java: DrawerActivity.java:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.switch_item) {
        return false;
    }

    closeDrawer();
    return true;
}

I did something like this.我做了这样的事情。

navigationView.getMenu().findItem(R.id.vibrate)
            .setActionView(new Switch(this));

    Switch vibrateSwitch =(Switch) 
navigationView.getMenu().findItem(R.id.vibrate).getActionView();
    vibrateSwitch.setChecked(true);
    vibrateSwitch.setOnCheckedChangeListener(new 
CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
isChecked){
                SharedPreferences sharedPreferences = 
getSharedPreferences(getString(R.string.MyPREFERENCES), MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean(getString(R.string.VIBRATE), isChecked);
                editor.commit();
        }

    });

For those of you using Kotlin Extensions对于那些使用Kotlin Extensions

  1. Menu file菜单文件
<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">

    <item
        android:id="@+id/nav_notifications_switch"
        android:icon="@drawable/ic_notifications"
        android:title="@string/notifications"
        app:actionLayout="@layout/drawer_notifications_switch" />

</menu>
  1. Switch layout file切换布局文件
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toggleSwitch"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" />
  1. Access in code代码访问
nav_view.menu.findItem(R.id.nav_notifications_switch)
    .actionView
    .toggleSwitch
    .setOnCheckedChangeListener { _, isChecked ->
        
    }

simply yes you can do this easily, i will post my code so you can implement it是的,您可以轻松做到这一点,我将发布我的代码,以便您可以实现它

<item android:id="@+id/nav_item_1"
        android:title="Android"
        android:icon="@drawable/ic_android_black_24dp"
        android:checked="true"/>

and the Main Activity Layout should be :主要活动布局应该是:

<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"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    <TextView
        android:id="@+id/content_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:text="Your Content Goes Here"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/colorAccent" />

    <!-- your content layout -->

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/menu_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_header"
    app:menu="@menu/menu_drawer" />

And here's a step by step tutorial for Navigation Drawer using Design Library这是使用设计库的导航抽屉的分步教程

You should be able to.你应该能够。

Your navigation drawer view can be a LinearLayout , and inside that, you can include your NavigationView and another ViewGroup adding the switches.你的导航抽屉视图可以是一个LinearLayout ,在里面,你可以包含你的NavigationView和另一个添加开关的ViewGroup

If you have to update the switch in navigation with respect to it's toggling then it's better to use two different menu layouts with checked switch and without checked switch.如果您必须在导航中更新切换开关,那么最好使用两种不同的菜单布局,选中开关和不选中开关。 So by checking the current state you can easily change the navigation drawer.因此,通过检查当前状态,您可以轻松更改导航抽屉。

In res -> menu -> menu.xml create <item> with field: app:actionViewClassres -> menu -> menu.xml create <item> with field: app:actionViewClass
example:例子:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:showIn="navigation_view">

        <item android:title="">
            <menu>
                <group android:checkableBehavior="single">

                    <item
                        android:checkable="false"
                        android:id="@+id/is_night_mode"
                      android:icon="@drawable/main_navigation_night_mode_light"
                    android:title="@string/main_navigation_night_mode"
                    app:actionViewClass="com.google.android.material.switchmaterial.SwitchMaterial" />
        

                </group>
            </menu>
        </item>
    </menu>

Next in Activity or Fragment find actionView like this:接下来在ActivityFragment中找到actionView是这样的:

val nightModeSwitch = binding.navView.menu.findItem(R.id.is_night_mode).actionView as SwitchMaterial

Set OnCheckedChangeListener设置OnCheckedChangeListener

nightModeSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
    //do something
}

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

相关问题 新设计中的Android switch Fragments支持导航抽屉 - Android switch Fragments in the new design support navigation drawer 如何通过导航抽屉切换片段(Android的新支持库)? - How to Switch Fragments by Navigation Drawer (new support library by android)? 使用android设计支持库的从右到左导航抽屉菜单 - Right to left navigation drawer menu using android design support library 如何使用 Android 设计支持库创建导航抽屉? - How to Create Navigation Drawer Using Android Design Support Library? 如何使用设计支持库在Android中的导航抽屉中添加页脚? - How to add footer to Navigation Drawer in Android using design support library? Android设计支持导航抽屉巨大的动作栏 - Android design support navigation drawer huge actionbar Android设计支持导航抽屉在底部对齐 - Android design support navigation drawer align on bottom 如何更改设计支持库的导航抽屉菜单项布局的部分 - How to change parts of the Navigation Drawer menu-item layouts of the Design Support Library Android设计支持库辅助抽屉菜单 - Android Design Support Library Secondary Drawer Menu Android支持库23.2-导航抽屉和/或工具栏 - Android Support Library 23.2 - Navigation Drawer and/or Toolbar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM