Switch in Navigation drawer item with Design Support Library on Android

前端 未结 8 1896
长情又很酷
长情又很酷 2020-12-30 07:05

I need to put Switch inside item in navigation drawer. I\'m using new design support library, but I cannot find if it is posibble at all. When using

android         


        
相关标签:
8条回答
  • 2020-12-30 07:59

    For those of you using 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 ->
            
        }
    
    0 讨论(0)
  • 2020-12-30 08:01

    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.

    0 讨论(0)
提交回复
热议问题