简体   繁体   English

导航视图菜单项不响应任何点击

[英]Navigation View Menu Item Doesn't respond to any Click

i want to know what is wrong with my code as there is no any responding to my clicks on the menu item我想知道我的代码有什么问题,因为我对菜单项的点击没有任何响应

class Main : AppCompatActivity() , NavigationView.OnNavigationItemSelectedListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        setSupportActionBar(mainToolbar)

        supportActionBar?.title = ""
        val toolbarToggle = ActionBarDrawerToggle(this,DrawerLayout,mainToolbar,R.string.drawer_open,R.string.drawer_close)
        DrawerLayout.addDrawerListener(toolbarToggle)
        toolbarToggle.syncState()

        mainNavigView.setNavigationItemSelectedListener(this)

    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        var titleT = item.title
        Toast.makeText(this,titleT, Toast.LENGTH_LONG).show()
        return true
    }

}

this is xml code for navigation and drawer这是导航和抽屉的 xml 代码


<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/DrawerLayout"
    android:background="@color/spark_bg"
    tools:context=".Main">

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/mainNavigView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigationheader"
        app:menu="@menu/navigationmenu"
         />

this is menu item code that contains id for the item i wanted to press这是包含我要按的项目的 ID 的菜单项代码

<menu xmlns:android="http://schemas.android.com/apk/res/android">
            <item
                android:id="@+id/myOrdersMI"
                android:icon="@drawable/ic_list"
                android:title="My Orders"
                />

</menu>

To use a DrawerLayout , position your primary content view as the first child with width and height of match_parent and no <layout_gravity>要使用DrawerLayout , position 您的主要内容视图作为第一个子视图,其宽度和高度为match_parent并且没有<layout_gravity>

    <androidx.drawerlayout.widget.DrawerLayout
       ...>

        <!-- main content goes here -->

        <!-- NavigationView -->
        <com.google.android.material.navigation.NavigationView
            android:layout_gravity="start|left"
            ../>

    </androidx.drawerlayout.widget.DrawerLayout>

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

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