gpt4 book ai didi

android - MenuItem 的选中状态未通过其图标正确显示

转载 作者:IT老高 更新时间:2023-10-28 23:27:32 24 4
gpt4 key购买 nike

我以这种方式定义了 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>

但是,当我使用它创建选项菜单时,即使 MenuItemisChecked() 属性为 true,图标也不会显示为选中状态.

我正在使用 ActionBarSherlock但是,如果我只是创建一个普通的选项菜单并调用 setChecked(true),我会得到相同的结果。无论项目的选中状态如何,它仍然显示 btn_star_off 可绘制对象。

onOptionsItemSelected()方法被正确调用,我可以成功更改checked属性:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.isCheckable()) {
item.setChecked(!item.isChecked());
}
return super.onOptionsItemSelected(item);
}

在此处设置断点显示 isChecked 属性正在更改,但图标本身并未更新以反射(reflect)正确的状态。

这里有什么我遗漏的吗?我做错了吗?我不明白为什么这不能正常工作。

最佳答案

根据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.

希望对你有帮助。

关于android - MenuItem 的选中状态未通过其图标正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6683186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com