我的 Android 应用中有以下菜单布局:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item1"
android:titleCondensed="Options"
android:title="Highlight Options"
android:icon="@android:drawable/ic_menu_preferences" />
<item android:id="@+id/item2"
android:titleCondensed="Persist"
android:title="Persist"
android:icon="@android:drawable/ic_menu_preferences"
android:checkable="true" />
</menu>
我的问题是,当我在 Android 模拟器中运行我的应用程序时,第二个菜单项似乎不是“可检查的”。该项目应该有一个绿色的勾号,对吧?表明它是可检查的。
我做错了吗?
最佳答案
布局看起来不错。但是你必须在代码中选中和取消选中菜单项。
来自 documentation :
When a checkable item is selected, the system calls your respective item-selected callback method (such as
onOptionsItemSelected()
). It is here that you must set the state of the checkbox, because a checkbox or radio button does not change its state automatically. You can query the current state of the item (as it was before the user selected it) withisChecked()
and then set the checked state withsetChecked()
.
关于Android 可检查菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6239163/