[英]Is it possible to Implement Toggle Button in Action Menu Item using Actionbar sherlock in android
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.我有一个应用程序,它在操作菜单项中有切换按钮,虽然我使用的是 Actionbar Sherlock,但我不知道如何在操作菜单项中放置切换按钮。 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.目的,如果我更改切换按钮的状态,它将根据字母表对人进行排序,并再次在出生日期中排序。
Thanks in Advance!提前致谢!
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);
}
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. 不要忘记像任何其他菜单按钮一样在xml中添加它,并配置
android:showAsAction
如果你想在溢出或外部显示它。
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_toogle"
android:showAsAction="ifRoom"
android:title="Share"
/>
</menu>
Other approach would be to use a custom layout for your ActionBar: 其他方法是为ActionBar使用自定义布局:
Basically you define a layout that contains your Toggle: 基本上你定义了一个包含你的Toggle的布局 :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ToggleButton
android:id="@+id/actionbar_service_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Logging On"
android:textOff="Logging Off" />
</RelativeLayout>
ALTERNATIVE 1: Then in your Activity or Fragment container you do: ALTERNATIVE 1:然后在您的Activity或Fragment容器中执行以下操作:
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.actionbar_top);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
...
ToggleButton button = (ToggleButton) findViewById(R.id.actionbar_service_toggle);
Notice that you are having a real ToggleButton and you are handling it in code as a real object ToggleButton, which has lots of advantages compared to having you re-implement your own toggle (theme, reliability, views hierarchy, native support...). 请注意,您正在使用真正的ToggleButton,并且您在代码中将其作为真实对象ToggleButton处理,与您重新实现自己的切换(主题,可靠性,视图层次结构,本机支持...)相比,它具有许多优点。 。
ALTERNATIVE 2: Another way to do it is embed your custom view into a regular menu view: 替代方案2:另一种方法是将自定义视图嵌入到常规菜单视图中:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/myswitch"
android:title=""
android:showAsAction="always"
android:actionLayout="@layout/actionbar_service_toggle" />
</menu>
If like me the actionLayout isn't working for you, try app:actionLayout="@layout/actionbar_service_toggle"
instead of android:actionLayout="@layout/actionbar_service_toggle"
as well as app:showAsAction="always"
instead of android:showAsAction="always"
This is because if you use appCompat the android namespace won't be used. 如果像我一样actionLayout不适合你,请尝试
app:actionLayout="@layout/actionbar_service_toggle"
而不是android:actionLayout="@layout/actionbar_service_toggle"
以及app:showAsAction="always"
而不是android:showAsAction="always"
这是因为如果使用appCompat,则不会使用android命名空间。
So here's the final version: 所以这是最终版本:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ToggleButton
android:id="@+id/actionbar_service_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Logging On"
android:textOff="Logging Off" />
</RelativeLayout>
and 和
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/myswitch"
android:title=""
app:showAsAction="always"
app:actionLayout="@layout/actionbar_service_toggle" />
</menu>
create xml file in menu: 在菜单中创建xml文件:
menu.xml menu.xml文件
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="si.ziga.switchinsideab.MainActivity">
<item android:id="@+id/switchId" android:title="" android:showasaction="always" android:actionlayout="@layout/switch_layout">
<item android:id="@+id/action_settings" android:orderincategory="100" android:title="@string/action_settings" app:showasaction="never">
</item></item></menu>
And then navigate to your layout folder make a new xml file and name it switch_layout.xml Here's the code: switch_layout.xml 然后导航到您的布局文件夹,创建一个新的xml文件并将其命名为switch_layout.xml以下是代码:switch_layout.xml
<!--?xml version="1.0" encoding="utf-8"?-->
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="match_parent" android:orientation="horizontal">
<switch android:id="@+id/switchAB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true">
</switch></relativelayout>
In your MainActivity class copy and paste this code: 在您的MainActivity类中复制并粘贴此代码:
MainActivity.java MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
switchAB = (Switch)menu.findItem(R.id.switchId)
.getActionView().findViewById(R.id.switchAB);7
Here somthing easy这里很简单
<?xml version="1.0" encoding="utf-8"?>
<item
android:title="@string/mode"
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:switchMinWidth="56dp"
android:layout_marginTop="120dp"
app:showAsAction="ifRoom"
app:actionViewClass="androidx.appcompat.widget.SwitchCompat"
android:checked="false"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.