[英]Icons on ToolBar Not Responding When Navigation Drawer Added
我在“活動”中添加了一個“導航抽屜”,它似乎運行良好。 問題是我的工具欄上還有一個名為info的圖標,該圖標現在沒有響應。 此信息項存在於我的menu.xml文件中:
<item
android:id="@+id/info"
android:title="Info"
android:icon="@drawable/ic_info_outline_black_24dp"
android:orderInCategory="100"
app:showAsAction="always" />
我在Java代碼中添加了此菜單:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar_items, menu);
return true;
}
並將其添加到我的onOptionsItemsSelectedMethod中,其中還包含我的導航抽屜:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.home:{
if (drawerLayout.isDrawerOpen(drawerList)){
drawerLayout.closeDrawer(drawerList);
}else{
drawerLayout.openDrawer(drawerList);
}
return true;
}case R.id.info:{
Toast.makeText(getApplicationContext(),"HEY",Toast.LENGTH_LONG).show();
}
default:return super.onOptionsItemSelected(item);
}
}
該圖標會在我的工具欄上彈出,但是當我單擊它時沒有響應。 請幫忙。 謝謝。
ID需要保持一致
在xml中,您有:
android:id="@+id/infoActionBar"
在Java代碼中,您正在尋找:
case R.id.info:
更改其中任何一個,以與另一個匹配。
編輯:用case android.R.id.home:
替換case R.id.home:
case android.R.id.home:
您在xml中的信息按鈕ID為android:id="@+id/infoActionBar"
並且您嘗試調用case R.id.info:
所以請更改其中一個case R.id.info:
試:)
infoActionBar
與 info
在onOptionsItemSelected
它必須是項目的ID,而不是item.getItemId()
的標題
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.home:{
if (drawerLayout.isDrawerOpen(drawerList)){
drawerLayout.closeDrawer(drawerList);
}else{
drawerLayout.openDrawer(drawerList);
}
return true;
}case R.id.infoActionBar:{
Toast.makeText(getApplicationContext(),"HEY",Toast.LENGTH_LONG).show();
}
default:return super.onOptionsItemSelected(item);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.