Android操作栏可检查菜单项无法正常工作/显示?
因此,我试图让我的菜单项,即显示在操作栏上的行为类似于可检查的菜单选项。第一部分工作,这意味着它是可检查的,当我按下它,并在代码中设置setChecked(true)时,它工作。但不起作用的是视觉部分。在选中和未选中状态下,菜单项在操作栏上的外观没有变化?我试着使用invalidateOptionsMenu(),但这不起作用,而且不仅如此,代码中的这一行我无法脱离选中状态?!? 发生的情况是,invalidate options menu()会取消选中状态,我会结束“循环”,或者每次按下菜单项时,我都会转到代码中未选中的部分,在那里它会被选中,而使用invalidate它会被取消选中,我想 以下是菜单“我的XML文件”中的代码:Android操作栏可检查菜单项无法正常工作/显示?,android,menu,android-actionbar,Android,Menu,Android Actionbar,因此,我试图让我的菜单项,即显示在操作栏上的行为类似于可检查的菜单选项。第一部分工作,这意味着它是可检查的,当我按下它,并在代码中设置setChecked(true)时,它工作。但不起作用的是视觉部分。在选中和未选中状态下,菜单项在操作栏上的外观没有变化?我试着使用invalidateOptionsMenu(),但这不起作用,而且不仅如此,代码中的这一行我无法脱离选中状态?!? 发生的情况是,invalidate options menu()会取消选中状态,我会结束“循环”,或者每次按下菜单项时
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/lenslist_menu_add"
android:showAsAction="always"
android:title="@string/add"/>
<item android:id="@+id/lenslist_menu_delete"
android:showAsAction="always"
android:checkable="true"
android:title="@string/delete"/>
</menu>
谢谢
可检查项仅出现在子菜单或上下文菜单中
您将它们用作主菜单项,因此它将不起作用
来源:下载API演示,打开文件ApiDemos/res/menu/checkable.xml,您将在第13行看到它作为注释。我不知道为什么他们在开发人员文档中没有提到这一点
带注释的参考:
最好的解决方案是将
的操作布局设置为复选框。此解决方案为您提供了一个具有本地外观的复选框(带有材质动画等),其字体与其他项目相匹配,并且它既可以作为动作也可以在子菜单中工作
创建一个名为action\u checkbox.html的新布局
:
在您的代码中,当创建菜单时,我们需要a)设置复选框的标题以匹配菜单项标题,b)恢复菜单可检查复选框和额外复选框的选中状态,以及c)为额外复选框添加onClicked()侦听器。在这段代码中,我将复选框的状态持久化为一个RetainedFragment
还是自己动手
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.item1).setIcon(menu_checked?R.drawable.menu_ico_checked:R.drawable.menu_ico_unchecked);
return super.onPrepareOptionsMenu(menu);
}
在OnOptions ItemSelected中执行以下操作:
....
menu_checked=!menu_checked;
invalidateOptionsMenu();
您可以在Api演示中找到注释:ApiDemos\res\menu\checkable.xml,我在开发人员文档中找不到它mRetainedFragment是什么?这正是我想要我的代码做的。当复选框被切换时,将其替换为您希望代码执行的任何操作。谢谢。我通过在操作栏中添加自定义布局进行了修复。完美地工作
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/menu_action_logging"
android:title="@string/action_logging"
android:orderInCategory="100"
android:showAsAction="always"
android:checkable="true"
android:checked="false"
android:actionLayout="@layout/action_checkbox"
/>
</menu>
// Set the check state of an actionbar item that has its actionLayout set to a layout
// containing a checkbox with the ID action_item_checkbox.
private void setActionBarCheckboxChecked(MenuItem it, boolean checked)
{
if (it == null)
return;
it.setChecked(checked);
// Since it is shown as an action, and not in the sub-menu we have to manually set the icon too.
CheckBox cb = (CheckBox)it.getActionView().findViewById(R.id.action_item_checkbox);
if (cb != null)
cb.setChecked(checked);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.menu_main, menu);
super.onCreateOptionsMenu(menu, inflater);
// Restore the check state e.g. if the device has been rotated.
final MenuItem logItem = menu.findItem(R.id.menu_action_logging);
setActionBarCheckboxChecked(logItem, mRetainedFragment.getLoggingEnabled());
CheckBox cb = (CheckBox)logItem.getActionView().findViewById(R.id.action_item_checkbox);
if (cb != null)
{
// Set the text to match the item.
cb.setText(logItem.getTitle());
// Add the onClickListener because the CheckBox doesn't automatically trigger onOptionsItemSelected.
cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOptionsItemSelected(logItem);
}
});
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_action_logging:
// Toggle the checkbox.
setActionBarCheckboxChecked(item, !item.isChecked());
// Do whatever you want to do when the checkbox is changed.
mRetainedFragment.setLoggingEnabled(item.isChecked());
return true;
default:
break;
}
return false;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.item1).setIcon(menu_checked?R.drawable.menu_ico_checked:R.drawable.menu_ico_unchecked);
return super.onPrepareOptionsMenu(menu);
}
....
menu_checked=!menu_checked;
invalidateOptionsMenu();