首页 > 解决方案 > 如何使我的抽屉布局中包含的按钮可点击?

问题描述

我想单击抽屉布局中包含的按钮。我该怎么做?

这是xml文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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=".main.views.MainActivity"
tools:openDrawer="start">

<include
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    layout="@layout/activity_home"></include>

<androidx.appcompat.widget.Toolbar
    android:id="@+id/ts_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"></androidx.appcompat.widget.Toolbar>

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_header"
    app:itemIconSize="35dp"
    app:itemTextAppearance="@style/TextAppearance.AppCompat.Medium.Inverse"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/nav_menu"></com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

标签: javaandroidandroid-studiokotlin

解决方案


val navView = findViewbyId(R.id.nav_View)
navView.setNavigationItemSelectedListener { item ->
    when(item.itemId) {
        R.id.YOUR_ID -> {
            //click
        }
    }

    true
}

推荐阅读