带有 actionLayout 的 Android 操作栏菜单项无法正常工作 [英] Android action bar menu item with actionLayout not working properly

查看:27
本文介绍了带有 actionLayout 的 Android 操作栏菜单项无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在开发一个 Android 应用程序.在我的应用程序中,我使用了 Sherlock 操作.我在操作栏中定义了几个菜单项,如下所示

Hi I am developing an Android application. In my application I am using Sherlock action. I've defined few menu items in action-bar like in following manner

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/card_menu"
        android:actionLayout="@layout/action_button"
        android:showAsAction="always"
        android:title="cards">
        <menu>
            <item
                android:id="@+id/C1"
                android:title="C1"/>
            <item
                android:id="@+id/C2"
                android:title="c2"/>
            <item
                android:id="@+id/C3"
                android:title="C3"/>
        </menu>
    </item>
    <item
        android:id="@+id/notification"
        android:actionLayout="@layout/notification_icon"
        android:icon="@drawable/notification"
        android:showAsAction="always"
        android:title="Notifications"/>

    <item
        android:id="@+id/filter"
        android:icon="@drawable/filter"
        android:showAsAction="always"
        android:title="Filter"/>
</menu>

我的 action_button 看起来像:

and My action_button looks like :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/menu_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/spinner_ab_focused_maroon"/>
    <TextView
        android:id="@+id/menu_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/imageView0"
        android:text="C1"/>
</RelativeLayout>

现在一切都显示出来了,但我的问题是当我点击 card_menu item 时,我定义了子菜单并定义了动作布局.它没有显示那些子菜单.我的其他菜单项工作正常.仅当我为包含无法显示子菜单的子菜单的项目定义操作布局时.如果我删除动作布局它工作正常.

Now everything is displayed but my problem is that when I click on card_menu item where I define sub menus and also define action layout. It's not showing those sub menus. My other menu items are working properly. Only when I define action layout for my item which contains sub menus that I am not able to display sub-menu. If I remove action layout it works fine.

我知道如果我们为项目定义动作布局,那么我们必须手动处理点击监听器.我是按照以下方式做到的

I know if we define action layout for item then we have to manually handle click listener. I did that in following manner

final MenuItem item = menu.findItem(R.id.card_menu);
        item.getActionView().setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onOptionsItemSelected(item);
                Toast.makeText(getActivity(), "click on menu", Toast.LENGTH_SHORT).show();
            }
        });

我能够处理该项目的点击事件,但无法显示下拉子菜单项.

I am able to handle to click event for that item but not able to show drop-down sub menu items.

我该如何解决这个问题?

How do I solve this problem?

推荐答案

在您的活动中试试这个代码.
请务必正确设置您的

Try this code in your activity.
Be sure to properly set your

R.menu.menuidentifier

R.menu.menuidentifier

R.id.menuitemidentifier

R.id.menuitemidentifier

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.actionbarhelpmenu, menu);
        final Menu m = menu;
        final MenuItem item = menu.findItem(R.id.ActionConnection);
        item.getActionView().setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {   
                m.performIdentifierAction(item.getItemId(), 0);
            }
        });
        return true;
    }

这篇关于带有 actionLayout 的 Android 操作栏菜单项无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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