菜单项的选中状态不被它的图标正确显示 [英] MenuItem's checked state is not shown correctly by its icon

查看:164
本文介绍了菜单项的选中状态不被它的图标正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有菜单项定义是这样的:

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.

推荐答案

据在<一个官方文件href="http://developer.android.com/guide/topics/ui/menus.html">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.

希望它帮助。

这篇关于菜单项的选中状态不被它的图标正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆