使用actionLayout的Android动作条菜单项不能正常工作

时间:2023-01-16 14:19:05

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

你好,我正在开发一个Android应用程序。在我的申请中,我使用了夏洛克动作。我在action-bar中定义了一些菜单项

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

and My action_button looks like :

我的action_button是:

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

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.

现在一切都显示出来了,但我的问题是,当我点击card_menu条目时,我定义子菜单并定义动作布局。它没有显示子菜单。我的其他菜单项正常工作。只有当我为包含子菜单的项定义操作布局时,我才能显示子菜单。如果我删除动作布局,它可以正常工作。

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?

我如何解决这个问题?

4 个解决方案

#1


48  

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

#2


6  

Accepted answer didn't work for me. My submenu behaved different on different devices. On Motorola Moto X it was like this: 使用actionLayout的Android动作条菜单项不能正常工作

接受的回答对我不起作用。我的子菜单在不同的设备上表现不同。摩托罗拉Moto X是这样的:

You can see that sub-menu is in wrong position (I clicked on bubble icon on the right of ActionBar).

您可以看到子菜单在错误的位置(我点击了ActionBar右边的bubble图标)。

So at the end I came up with different solution: use PopupMenu instead. Code looks like this:

最后我想到了不同的解决方案:用弹出菜单代替。代码是这样的:

@Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_fragment_chat, menu);
    final MenuItem item = menu.findItem(R.id.menu_item_actionbar_avatar);
    MenuItemCompat.getActionView(item).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showProfileMenuPopup(v);
        }
    });
}

public void showProfileMenuPopup(View v) {
    PopupMenu popup = new PopupMenu(getActivity(), v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu_avatar_actions, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            return false;
        }
    });
    popup.show();
}

And it worked :)

和它工作:)

#3


1  

Full working code

完整的工作代码

 <item
    android:title="search"
    android:id="@+id/mSearch"
    app:actionLayout="@layout/my_custom_menu_item"
    android:orderInCategory="100"
    app:showAsAction="always"/>

Code:

代码:

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.menu,menu); MenuItem item = menu.findItem(R.id.mSearch);
    ImageView iv= (ImageView) item.getActionView().findViewById(R.id.search_1);
    iv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context, "clicked", Toast.LENGTH_SHORT).show();
        }
    });
    return true;
}

#4


0  

Refer to the answer

参考答案

If there is a button in the action layout, in order to get the callback from menu item, don't forget to set the button

如果动作布局中有一个按钮,为了从菜单项获取回调,不要忘记设置按钮

android:clickable="false"

#1


48  

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

#2


6  

Accepted answer didn't work for me. My submenu behaved different on different devices. On Motorola Moto X it was like this: 使用actionLayout的Android动作条菜单项不能正常工作

接受的回答对我不起作用。我的子菜单在不同的设备上表现不同。摩托罗拉Moto X是这样的:

You can see that sub-menu is in wrong position (I clicked on bubble icon on the right of ActionBar).

您可以看到子菜单在错误的位置(我点击了ActionBar右边的bubble图标)。

So at the end I came up with different solution: use PopupMenu instead. Code looks like this:

最后我想到了不同的解决方案:用弹出菜单代替。代码是这样的:

@Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_fragment_chat, menu);
    final MenuItem item = menu.findItem(R.id.menu_item_actionbar_avatar);
    MenuItemCompat.getActionView(item).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showProfileMenuPopup(v);
        }
    });
}

public void showProfileMenuPopup(View v) {
    PopupMenu popup = new PopupMenu(getActivity(), v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu_avatar_actions, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            return false;
        }
    });
    popup.show();
}

And it worked :)

和它工作:)

#3


1  

Full working code

完整的工作代码

 <item
    android:title="search"
    android:id="@+id/mSearch"
    app:actionLayout="@layout/my_custom_menu_item"
    android:orderInCategory="100"
    app:showAsAction="always"/>

Code:

代码:

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.menu,menu); MenuItem item = menu.findItem(R.id.mSearch);
    ImageView iv= (ImageView) item.getActionView().findViewById(R.id.search_1);
    iv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context, "clicked", Toast.LENGTH_SHORT).show();
        }
    });
    return true;
}

#4


0  

Refer to the answer

参考答案

If there is a button in the action layout, in order to get the callback from menu item, don't forget to set the button

如果动作布局中有一个按钮,为了从菜单项获取回调,不要忘记设置按钮

android:clickable="false"