是否可以在Android中使用Actionbar Sherlock在Action菜单项中实现切换按钮 [英] Is it possible to Implement Toggle Button in Action Menu Item using Actionbar sherlock in android
问题描述
我有一个应用,该应用的动作菜单项中有切换按钮,尽管我使用的是Actionbar Sherlock,但我不知道如何将切换按钮置于动作菜单项中。我不想作为自定义布局放置在操作栏中,但是我想将其作为菜单项放置。如果有人找到解决方案,请帮助我。
I have an app, which have toggle button in action menu item, though i'm using Actionbar Sherlock, I don't know, how to place the toggle button in the action menu item. I don't want to place as a custom layout in action bar, but i want to place it as a Menu item. If anyone find solution, Please help me out.
目的是,如果我更改切换按钮的状态,它将根据字母顺序对该人进行排序,并再次在出生日期中对其进行排序。
Purpose, If I change the state of toggle button, it will sort the person based on ALphabets and again in Date of Birth.
预先感谢!
推荐答案
就像普通的菜单按钮一样添加它,并用布尔值检查它的状态变量,则可以在更改排序方式时更改图标和标题
Just add it like a normal Menu Button, check its state with a boolean variable, and you can change the icon and title when changing the sortmode
boolean birthSort=false;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_toggle:
if(birthSort){
//change your view and sort it by Alphabet
item.setIcon(icon1)
item.setTitle(title1)
birthSort=false;
}else{
//change your view and sort it by Date of Birth
item.setIcon(icon2)
item.setTitle(title2)
birthSort=true;
}
return true;
}
return super.onOptionsItemSelected(item);
}
别忘了添加xml,就像其他任何菜单按钮一样,如果要在溢出中或外部显示它,请配置 android:showAsAction
。
Don't forget to add it in xml like any other menu button and configure android:showAsAction
if you want to show it in overflow or outside of it.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_toogle"
android:showAsAction="ifRoom"
android:title="Share"
/>
</menu>
这篇关于是否可以在Android中使用Actionbar Sherlock在Action菜单项中实现切换按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!