MenuItem 的选中状态未通过其图标正确显示
我以这种方式定义了 MenuItem:
<?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>
但是,当我使用此方法创建选项菜单时,图标永远不会以选中状态显示,即使 MenuItem
的 isChecked()
属性为 true。
我正在使用 ActionBarSherlock 控件,但是,如果我只是创建一个普通的选项菜单并调用,我会得到相同的结果setChecked(true)
。无论项目的选中状态如何,它仍然显示 btn_star_off
可绘制对象。
onOptionsItemSelected()
方法被正确调用,并且我可以成功更改选中的属性:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.isCheckable()) {
item.setChecked(!item.isChecked());
}
return super.onOptionsItemSelected(item);
}
在此处设置断点显示 isChecked 属性正在更改,但图标本身未更新以反映正确的状态。
我在这里缺少什么吗?我这样做错了吗?我不明白为什么这不能正常工作。
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>
and btn_star.xml
defined this way:
<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>
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.
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.
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);
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据官方文档 http://developer.android.com/guide/topics /ui/menus.html
希望它有帮助。
According to the official document at http://developer.android.com/guide/topics/ui/menus.html
Hope it helps.
如果您仍然希望在 xml 可绘制对象中定义行为(选中或未选中),这是实现此目的的一种方法:
If you still want to have the behavior (checked, not checked) defined in a xml drawable, this is one way you could accomplish this:
更简单的方法(没有 xml-states 文件):
A bit simpler way (without xml-states file):
问题有点老了,但我最近偶然发现了这个问题。
经过一些分析后发现,菜单项的选中状态没有正确传播到可绘制对象。这是我想出的解决方案(在 Kotlin 中)。
确保您的
menuItem
使用状态列表可绘制对象(如问题中的btn_star.xml
),然后创建一个可绘制包装器类:最后一步是用包装的可绘制对象替换可绘制菜单项:
之后,在更改菜单项的选中状态时,您不需要执行任何操作,图标应该具有自我意识,并在选中状态发生变化时做出反应。
Question is a bit old but I stumbled upon this problem recently.
After some analyzing it turns out that checked state of menu item is not being properly propagated down to the drawable. Here's the solution I came up with (in Kotlin).
Ensure your
menuItem
is using a state list drawable (likebtn_star.xml
from question), then create a drawable wrapper class:Last step is replacing menu items drawable with wrapped drawable:
After that you don't need to do anything when changing menu items checked state, icon should be self aware and react whenever checked state changes.