我有菜单项定义是这样的:
I have MenuItem defined this way:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_starred"
android:icon="@drawable/btn_star"
android:title="@string/description_star"
android:checkable="true"
android:checked="true"
android:orderInCategory="1"
android:showAsAction="always" />
</menu>
和 btn_star.xml
定义是这样的:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="false"
android:drawable="@drawable/btn_star_off_normal" />
<item
android:state_checked="true"
android:drawable="@drawable/btn_star_on_normal" />
</selector>
当我创建一个使用此选项菜单,但是,图标是从来没有在它的选中状态显示,即使菜单项
的器isChecked ()
属性为true。
When I create an options menu using this, however, the icon is never shown in its checked state, even if the MenuItem
's isChecked()
property is true.
我使用的是 ActionBarSherlock 的控制,但是,我得到了同样的结果,如果我简单地创建一个正常的选项菜单,并调用 setChecked(真)
。不管项目的选中状态的它仍然显示了 btn_star_off
绘制。
I'm using the ActionBarSherlock control, however, I'm getting the same result if I simply create a normal options menu and call setChecked(true)
. It still displays the btn_star_off
drawable regardless of the checked state of the item.
在 onOptionsItemSelected()
方法被调用正确的,我可以成功改变checked属性:
The onOptionsItemSelected()
method is being called correctly, and I can successfully change the checked property:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.isCheckable()) {
item.setChecked(!item.isChecked());
}
return super.onOptionsItemSelected(item);
}
在这里设置一个断点显示了物业器isChecked被改变,但是图标本身不更新,以反映正确的状态。
Setting a breakpoint here shows the isChecked property being changed, but the icon itself is not updated to reflect the correct state.
有我丢失的东西吗?我这样做不正确的?我想不通为什么会无法正常工作。
Is there something I'm missing here? Am I doing this incorrectly? I can't figure out why this wouldn't be working correctly.
据在http://developer.android.com/guide/topics/ui/menus.html
注:菜单中的图标菜单(从选项菜单)项目不能 显示一个复选框或单选按钮。如果你选择做的项目 图标菜单可检查的,您必须手动指示由选中状态 交换每次状态变化中的图标和/或文字。
Note: Menu items in the Icon Menu (from the Options Menu) cannot display a checkbox or radio button. If you choose to make items in the Icon Menu checkable, you must manually indicate the checked state by swapping the icon and/or text each time the state changes.
希望它帮助。