我的 OptionMenu 中有一个按钮,当触摸它时会打开一个弹出菜单,其中包含在运行时获取并以编程方式添加的项目(因此不能使用硬编码的 xml 菜单项)。我想根据它们的值突出显示这些项目的一个子集,我使用了这里建议的内容:如何在 NavigationView 中自定义项目背景和项目文本颜色?尝试使项目具有不同的颜色。但是,尽管isChecked()价值不同,但所有项目的颜色都相同。
这是该问题的一个小工作示例:
MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.actionbar_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case R.id.action_bar_button:{
showMenu();
return true;
}
}
return super.onOptionsItemSelected(item);
}
private void showMenu() {
View v = findViewById(R.id.action_bar_button);
Context wrapper = new ContextThemeWrapper(this, R.style.MyPopupMenu);
PopupMenu popupMenu = new PopupMenu(wrapper, v);
//Sample items to demonstrate the issue. I want the background …Run Code Online (Sandbox Code Playgroud)