Android 单击操作栏中的按钮时弹出菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10483612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:56:18  来源:igfitidea点击:

Popup menu on click of a button in action Bar

androidpopupandroid-actionbar

提问by darsh

I am trying to implement an action bar in which one of the buttons on click shows a popup menu. Here's the menu. XML (menu items in the action bar)

我正在尝试实现一个操作栏,其中单击按钮之一显示弹出菜单。这是菜单。XML(操作栏中的菜单项)

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

<item
    android:id="@+id/search"
    android:icon="@drawable/ic_action_search"
    android:orderInCategory="0"
    android:showAsAction="always"
    android:title="@string/menu_search"/>
<item
    android:id="@+id/refresh"
    android:icon="@drawable/ic_action_refresh"
    android:orderInCategory="1"
    android:showAsAction="always"
    android:title="@string/menu_refresh"/>


 <Item
    android:id="@+id/popup"
    android:icon="@drawable/ic_action_search"
    android:onClick="showPopup"
    android:orderInCategory="1"
    android:showAsAction="always"
    android:title="@string/menu_search" />

I wish to show a popup menu on the click of the item having id "@+id/popup".

我希望在单击 id 为“@+id/popup”的项目时显示一个弹出菜单。

here is the XML for the popup menu

这是弹出菜单的 XML

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

<item
    android:id="@+id/item1"
    android:icon="@drawable/ic_action_search"
    android:orderInCategory="0"
    android:showAsAction="always"
    android:title="@string/menu_search"/>
<item
    android:id="@+id/item2"
    android:icon="@drawable/ic_action_search"
    android:orderInCategory="1"
    android:showAsAction="always"
    android:title="@string/menu_search"/>

here is the onClick method for the button

这是按钮的 onClick 方法

public void showPopup(View v) {
    PopupMenu popup = new PopupMenu(this, v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.overflow, popup.getMenu());
    popup.show();
}

And the problem is that no popup shows up on click of that button. Need help folks.

问题是单击该按钮时没有弹出窗口。需要帮助的人。

采纳答案by darsh

I found a solution to this. Instead to using the menu XML to inflate the popup menu, I made a XML layout file.

我找到了解决方案。我没有使用菜单 XML 来扩展弹出菜单,而是创建了一个 XML 布局文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8b8989"
android:orientation="vertical"
android:padding="10dip" >

<TextView
    android:id="@+id/menuItem1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:text="@string/menu1" />

<TextView
    android:id="@+id/menuItem2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:text="@string/menu2" />
<TextView
    android:id="@+id/menuItem3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:text="@string/menu3" />
</LinearLayout> 

and i changed the onClick method

我改变了 onClick 方法

public void showPopup(View v) {
    LayoutInflater inflater = (LayoutInflater) MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        PopupWindow pw = new PopupWindow(inflater.inflate(
                R.layout.overflow_layout, null, false), 300, 400, true);
        pw.showAtLocation(findViewById(R.id.container), Gravity.CENTER, 0,
                0);
}

This solved the issue

这解决了这个问题

回答by Nlinscott

I found this here: http://developer.android.com/guide/topics/ui/menus.html

我在这里找到了这个:http: //developer.android.com/guide/topics/ui/menus.html

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

    <item android:id="@+id/selectImg"
        android:icon="@android:drawable/ic_dialog_dialer"
        android:showAsAction="always">

        <menu>
            <item android:id="@+id/top"
                android:title="@string/topimg"/>
            <item android:id="@+id/bottom"
                android:title="@string/botimg" />
        </menu>

    </item>
</menu>

You can put a menu within a menu to present sub-menus when the item is clicked. Then, in Java, you can use the same methods as usual.

您可以在菜单中放置一个菜单以在单击该项目时显示子菜单。然后,在 Java 中,您可以像往常一样使用相同的方法。

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
       // View v = findViewById(R.id.f);
        switch (item.getItemId()) {
            case R.id.top:
                //action
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

The id of 'top' in the xml is still recognized even though it is a sub menu. This worked for me and it looks just like the popup menu.

即使它是一个子菜单,xml 中 'top' 的 id 仍然可以识别。这对我有用,它看起来就像弹出菜单。

回答by serabile

Hi every one, that's my own solution : i created the showPopup method, and then i called it in the onOptionsItemSelected like this :

大家好,这是我自己的解决方案:我创建了 showPopup 方法,然后像这样在 onOptionsItemSelected 中调用它:

public void showPopup(){
    View menuItemView = findViewById(R.id.menu_save);
    PopupMenu popup = new PopupMenu(getActivity(), menuItemView);
    MenuInflater inflate = popup.getMenuInflater();
    inflate.inflate(R.menu.popup, popup.getMenu());
    popup.show();

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.menu_save:    
        showPopup();
        return true;
    default:
        return super.onOptionsItemSelected(item);
}

}

popup.xml

弹出窗口.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/decon"
    android:showAsAction="ifRoom"
    android:title="@string/decon"/>

<item
    android:id="@+id/mRes"
    android:showAsAction="ifRoom"
    android:title="@string/mesRes"/>

</menu>

main.xml => it's called onCreateOptionsMenu

main.xml => 它被称为 onCreateOptionsMenu

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

<item
    android:id="@+id/menu_save"
    android:enabled="true"
    android:icon="@drawable/action_save"
    android:showAsAction="ifRoom|withText"
    android:title="@string/action_save"
    android:visible="true"/>

</menu>

Finally i

最后我

implements PopupMenu.OnMenuItemClickListener to @Override onMenuItemClick method.

回答by Jeje Doudou

As the popup menu is a MENU, you have to handle this by implementing the "onOptionsItemSelected". You'll be able to say what to do for each menu option. It will replace the "onClick" option you defined and will be called automatically.

由于弹出菜单是一个 MENU,您必须通过实现“onOptionsItemSelected”来处理这个问题。您将能够说出每个菜单选项的操作。它将替换您定义的“onClick”选项并自动调用。

回答by Rookie

Try to change 'this' to getActivity().

尝试将“this”更改为 getActivity()。

public void showPopup(View v) {
    PopupMenu popup = new PopupMenu(getActivity(), v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.overflow, popup.getMenu());
    popup.show();
}

Hope it helps..!!

希望能帮助到你..!!

回答by user1049280

android:onClick="popup"

may be you should change it to android:onClick="showPopup"?

也许你应该把它改成android:onClick="showPopup"