效果
res下创建 menu 文件 下创建 menu.xml
<item android:id="@+id/menu_setting" android:title="设置" android:icon="@drawable/ic_baseline_settings_24" app:showAsAction="ifRoom" />
Java代码重写2个方法
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//绑定Menu
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu,menu);
return true;
}
//选中哪一项执行哪一项的操作
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
//拿到每一项的Id
int itemId = item.getItemId();
switch (itemId){
case R.id.menu_setting :
//操作······
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
标签:选项,菜单,onOptionsItemSelected,menu,item,boolean,Menu,Override
From: https://www.cnblogs.com/loulan/p/16974422.html