Android ActionBar setCheckable() 不工作
我试图使我的 Menu
上的其中一个 MenuItems
具有复选标记功能,但它似乎不起作用。所有其他 MenuItems
都可以工作,这个菜单项也可以,除了复选标记显示之外。我做错了什么?
MenuItem actionPickMode = menu.add(0, 3, 0, "pickmode");
actionPickMode.setTitle("Pick Mode");
actionPickMode.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT
| MenuItem.SHOW_AS_ACTION_ALWAYS);
actionPickMode.setVisible(true);
actionPickMode.setCheckable(true);
I'm trying to make one of the MenuItems
on my Menu
have a checkmark ability, but it doesn't seem to work. All other MenuItems
are working, this one does too, except for the checkmark display. What am I doing wrong?
MenuItem actionPickMode = menu.add(0, 3, 0, "pickmode");
actionPickMode.setTitle("Pick Mode");
actionPickMode.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT
| MenuItem.SHOW_AS_ACTION_ALWAYS);
actionPickMode.setVisible(true);
actionPickMode.setCheckable(true);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正在尝试向实际位于操作栏上的
MenuItem
添加复选标记。根据这个问题,这是不可能的: Android 操作栏可检查菜单项无法正常工作/显示?你可以自己实现 - 当单击该项目时,使用
setIcon
更改可绘制对象,并保持的状态自己切换。此问题描述了如何获取内置复选标记Drawable
:如何在Android操作系统中访问复选标记drawable?Looks like you're trying to add a checkmark to a
MenuItem
that is actually on the Action Bar. According to this question it isn't possible: Android action bar checkable menu item does not work/show properly?What you can do is implement it yourself--when the item is clicked, use
setIcon
to change the drawable, and maintain the state of the toggle yourself. This question describes how you can get the built-in checkmarkDrawable
s: How to access checkmark drawable in Android OS?您必须为包含复选框的操作创建自定义布局。请参阅我的回答此处。
You have to create a custom layout for your action containing a checkbox. See my answer here.