单击后如何保持溢出菜单? [英] How to hold the overflow menu after I click it?

查看:42
本文介绍了单击后如何保持溢出菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在溢出菜单中添加了一些复选框.单击溢出菜单中的复选框后,我希望溢出菜单保持不变,而不是消失.我怎样才能做到这一点?感谢您的帮助.

这是我的菜单xml文件.

 <项目android:id ="@ + id/action_check"android:title ="@ string/action_check"android:orderInCategory ="1"app:showAsAction ="never"android:visible ="true"android:checkable ="true"/>< item android:id ="@ + id/notification"android:orderInCategory ="2"android:title ="@ string/notification"app:showAsAction ="never"android:visible ="true"android:checkable ="true"/>< item android:id ="@ + id/about"android:orderInCategory ="3"android:title ="@ string/about"app:showAsAction ="never"></item> 

解决方案

这就是我的做法.

在执行选项菜单的活动中添加以下代码.

  @Overridepublic boolean onOptionsItemSelected(MenuItem item){//检查其他内容item.setChecked(!item.isChecked());//保持菜单的主要部分item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);item.setActionView(new View(this));item.setOnActionExpandListener(new MenuItem.OnActionExpandListener(){@Overridepublic boolean onMenuItemActionExpand(MenuItem item){返回false;}@Overridepublic boolean onMenuItemActionCollapse(MenuItem item){返回false;}});返回false;} 

通过添加以下行: item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); 我正在标记该项,因为它具有可扩展/可折叠的行为,因此它将调用 setOnActionExpandListener .

此行: item.setActionView(new View(this)); 是项目处于展开状态时的视图.它只是一个虚拟视图,因为我们永远不会让它扩展我的显示方式接下来要解释.

您看到我从 setOnActionExpandListener 的两种方法返回false来抑制项目的扩展和折叠,因此我们在上一步中给出的视图将永远不会显示,并且菜单将保持打开状态./p>

以下将是您的菜单文件:

 <菜单xmlns:android ="http://schemas.android.com/apk/res/android"xmlns:app ="http://schemas.android.com/apk/res-auto">< group android:checkableBehavior ="all"><项目android:id ="@ + id/action_check"android:orderInCategory ="1"android:title =标题1"app:showAsAction ="never"/><项目android:id ="@ + id/notification"android:orderInCategory ="2"android:title =标题2"app:showAsAction ="never"/><项目android:id ="@ + id/about"android:orderInCategory ="3"android:title =标题3"app:showAsAction ="never"/></group></菜单> 

请注意, group android:checkableBehavior ="all" 这一行是要告知该组中的所有项目都具有可检查的行为,因此您不必在每个项目中都编写可检查的内容.

I have added some checkboxs in my overflow menu. I want my overflow menu to hold rather than disappear once I click the checkbox in the overflow menu. How can I do that? Thanks for help.

This is my menu xml file.

<item
    android:id="@+id/action_check"
    android:title="@string/action_check"
    android:orderInCategory="1"
    app:showAsAction="never"
    android:visible="true"
    android:checkable="true"/>
<item android:id="@+id/notification"
    android:orderInCategory="2"
    android:title="@string/notification"
    app:showAsAction="never"
    android:visible="true"
    android:checkable="true"/>
<item android:id="@+id/about"
    android:orderInCategory="3"
    android:title="@string/about"
    app:showAsAction="never"></item>

解决方案

This is how i did it.

Add following code in your activity where option menu is implemented.

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //your checking other stuff
        item.setChecked(!item.isChecked());

        //main part for holding onto the menu
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
        item.setActionView(new View(this));
        item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                return false;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                return false;
            }
        });
        return false;
    }

By adding the line: item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); I am marking the item as it has expandable/collapsible behavior so it will call the setOnActionExpandListener.

This line here: item.setActionView(new View(this)); is a view when item is in expanded state.It is just a dummy view because we will never let it expand how i am going to explain next.

You see that i am returning false from both the methods of setOnActionExpandListener to suppress expansion and collapsing of the item so the view we gave in previous step would never show up and menu would remain open.

Following would be your menu file:

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

    <group android:checkableBehavior="all">


        <item
            android:id="@+id/action_check"
            android:orderInCategory="1"
            android:title="Title 1"
            app:showAsAction="never" />

        <item
            android:id="@+id/notification"
            android:orderInCategory="2"
            android:title="Title 2"
            app:showAsAction="never" />

        <item
            android:id="@+id/about"
            android:orderInCategory="3"
            android:title="Title 3"
            app:showAsAction="never" />

    </group>

</menu>

Notice the line group android:checkableBehavior="all" is to tell that all items in the group will have checkable behavior so that you don't have to write checkable in each item.

这篇关于单击后如何保持溢出菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆