本文介绍了动作条COMPAT不显示下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是动作条COMPAT在我的应用程序,我需要显示的下拉菜单中的动作条,当用户点击按钮。在4.x的版本中,它是确定的,但在2.3中我碰到这样的对话。

I'm using ActionBar Compat in my app and i need to show dropdown menu when user click button in actionbar. In 4.x versions it's ok, but in 2.3 i have something like dialog.

2.3截图

就像我说的,在4.x中它的确定:

Like i said, in 4.x it's ok:

下面是一些code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.categories, menu);
    return super.onCreateOptionsMenu(menu);
}

categories.xml

categories.xml

<?xml version="1.0" encoding="utf-8"?>
<menu   xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:onekr="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/refresh"
        android:icon="@drawable/ic_action_refresh"
        onekr:showAsAction="always"
        android:title="@string/refresh"
        android:visible="true">
    </item>
    <item
        android:id="@+id/dissmiss_page_loading"
        android:icon="@drawable/ic_action_cancel"
        onekr:showAsAction="always"
        android:title="@string/cancel"
        android:visible="false">
    </item>

    <item
        android:id="@+id/categories"
        android:icon="@drawable/ic_action_overflow"
        onekr:showAsAction="always"
        android:title="@string/categories"
        android:visible="true">
        <menu   xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:onekr="http://schemas.android.com/apk/res-auto" >
            <item
                android:id="@+id/city"
                onekr:showAsAction="always"
                android:title="@string/city"
                android:visible="true">
            </item>
            <item
                android:id="@+id/culture"
                onekr:showAsAction="always"
                android:title="@string/culture"
                android:visible="true">
            </item>
        </menu>
    </item>

</menu>

为什么这个可以吗?这可怎么解决吗?非常感谢所有谁回应!

Why does this can be? How this can be fixed? Big thanks to all who respond!

推荐答案

要避免这种情况,我们需要使用PopupMenu的!

To avoid this, we need use PopupMenu!

例如:

action_bar_buttons.xml

   <?xml version="1.0" encoding="utf-8"?>
    <menu   xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:mynamespace="http://schemas.android.com/apk/res-auto" >

        <item
            android:id="@+id/some_other_btn"
            android:icon="@drawable/some_other_btn"
            mynamespace:showAsAction="always"
            android:title="@string/some_other_btn"
            android:visible="true">
        </item>

        <item
            android:id="@+id/open_drop_down"
            android:icon="@drawable/open_drop_down"
            mynamespace:showAsAction="always"
            android:title="@string/open_drop_down"
            android:visible="true"/>

    </menu>

drop_down_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu   xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:mynamespace="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/item_1"
        android:title="@string/item_1"
        android:visible="true"
        mynamespace:showAsAction="always">
    </item>
    <item
        android:id="@+id/item_2"
        android:title="@string/item_1"
        android:visible="true"
        mynamespace:showAsAction="always">
    </item>

</menu>

MainActivity.java

public void showPopup(int itemId){
        View view = findViewById(itemId);
        PopupMenu popupMenu = new PopupMenu(getSupportActionBar().getThemedContext(), view);
        popupMenu.setOnMenuItemClickListener(/* drop_down item click listener */);
        popupMenu.getMenuInflater().inflate(R.menu.drop_down_menu, popupMenu.getMenu());
        popupMenu.show();
    }


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    /* . . . */

    case R.id.open_drop_down:
        showPopup(R.id.open_drop_down);
        break;

    /* . . . */

    default:
        break;
    }

    return super.onOptionsItemSelected(item);
}

这篇关于动作条COMPAT不显示下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:03