Android导航抽屉菜单项无法亮起选定项
第一项高亮显示,其余项无法亮起: 这是我的背景选择器:Android导航抽屉菜单项无法亮起选定项,android,navigation-drawer,Android,Navigation Drawer,第一项高亮显示,其余项无法亮起: 这是我的背景选择器: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/accent_translucent" android:state_pressed="true"/> <item android:drawable="@android:color/transparent"/>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/accent_translucent" android:state_pressed="true"/>
<item android:drawable="@android:color/transparent"/>
任何想法都会有帮助
如果我使用以下代码,我可以突出显示,但无法启动新活动
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
menuItem.setChecked(true);
drawerLayout.closeDrawers();
return true;
Android允许非常有限的菜单项访问。突出显示菜单项的最佳方法是在菜单中使用褪色的图标,如果要突出显示,只需将图标替换为颜色更鲜艳的图标即可
navigationView.getMenu().findItem(R.id.drawer_dashboard).setIcon(R.drawable.icon_highlighted_icon);
对于其余部分,重置正常图标
此外,请尝试您的旧代码-
onNavigationItemSelected(navigationView.getMenu().getItem(id));
navigationView.setCheckedItem(id);
此处将id替换为当前项目位置
希望它能帮助您:)创建视图时,请尝试使用getItem(int index)方法获取所有菜单项,然后调用setCheckable(true),确保它是可检查的。不知道,为什么要调用此行-onNavigationItemSelected(navigationView.getMenu().findItem(R.id.drawer\u dashboard));我尝试了堆栈中的代码。其余的代码属于我,已经更新了代码。你现在可以检查吗?更新了我的答案,请检查:)我正在从异常中获取错误索引…我没有声明数组。这是bcoz吗
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
menuItem.setChecked(true);
drawerLayout.closeDrawers();
return true;
navigationView.getMenu().findItem(R.id.drawer_dashboard).setIcon(R.drawable.icon_highlighted_icon);
onNavigationItemSelected(navigationView.getMenu().getItem(id));
navigationView.setCheckedItem(id);