导航视图菜单项不响应任何点击
Navigation View Menu Item Doesn't respond to any Click
我想知道我的代码有什么问题,因为我点击菜单项没有任何响应
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
}
}
这是 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"
/>
这是菜单项代码,其中包含我要按下的项目的 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>
要使用 DrawerLayout
,请将您的主要内容视图定位为宽度和高度为 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>
我想知道我的代码有什么问题,因为我点击菜单项没有任何响应
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
}
}
这是 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"
/>
这是菜单项代码,其中包含我要按下的项目的 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>
要使用 DrawerLayout
,请将您的主要内容视图定位为宽度和高度为 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>