是否有可能在Android的使用的ActionBar福尔摩斯来实现在操作菜单项切换按钮(Is it

2019-08-17 01:28发布

我有一个应用程序,它在操作菜单项切换按钮,虽然我使用的ActionBar福尔摩斯,我不知道,如何把切换按钮在操作菜单项。 我不希望把在操作栏自定义布局,但我想将它作为一个菜单项。 如果有人找到解决方案,请帮助我。

目的,如果我改变切换按钮的状态下,将排序基于字母,并再次在出生日期的人。

提前致谢!

Answer 1:

只需添加它像一个正常的菜单按钮,用布尔变量检查其状态,并改变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如果你想显示它溢出或它的外面。

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/menu_toogle"
    android:showAsAction="ifRoom"
    android:title="Share"
     />
</menu>


Answer 2:

另一种方法是使用自定义布局为你的动作条:

基本上你定义一个包含您切换布局

<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>

选择1:然后在你的活动片段容器,你这样做:

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);

请注意,你有一个真正的切换按钮,你正在处理它的代码作为一个真正的对象切换按钮,这有很多的优点相比,有你重新实现自己的切换(题材,可靠性,景色层次结构,原生支持...) 。

源代码在这里 。


选择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>


Answer 3:

如果像我这样actionLayout不为你工作,尝试app:actionLayout="@layout/actionbar_service_toggle" ,而不是android:actionLayout="@layout/actionbar_service_toggle"以及app:showAsAction="always" ,而不是android:showAsAction="always"这是因为如果使用程序兼容性将不会使用Android的命名空间。

因此,这里的最终版本:

<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>

<?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>


Answer 4:

创建菜单的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>

然后导航到你的布局文件夹中创建一个新的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>

在您的MainActivity类别复制并粘贴此代码:
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

或者点击此链接这些东西



文章来源: Is it possible to Implement Toggle Button in Action Menu Item using Actionbar sherlock in android