简体   繁体   English

Android如何更改弹出菜单项背景

[英]Android How to change Popup Menu Item Background

I am using a Popup menu. 我正在使用弹出菜单。 I want to change the background of popup menu but not finding a way to do it. 我想改变弹出菜单的背景,但没有找到办法。 I don't want alert dialog type functonality. 我不想要警报对话框类型功能。

I have a button and on the click listener I initialize a popup menu. 我有一个按钮,在点击监听器上我初始化一个弹出菜单。

        Button btn = (Button) findViewById(R.id.btn);

        OnClickListener listener = new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                popup = new PopupMenu(getBaseContext(), v);
                popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());
                popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem arg0) {
                        // TODO Auto-generated method stub
                        return true;
                    }
                });
                popup.show();
            }
        };
        btn.setOnClickListener(listener);

In the menu folder I have popup.xml. 在菜单文件夹中我有popup.xml。 I want to change the background of the items of popup menu(not the button).How can I do it? 我想改变弹出菜单项目的背景(而不是按钮)。我该怎么做?

I solved the problem using this code: 我用这段代码解决了这个问题:

Context wrapper = new ContextThemeWrapper(getContext(), R.style.PopupMenu);
PopupMenu popupMenu = new PopupMenu(wrapper, v);

reference: blog.stylingandroid.com 参考:blog.stylingandroid.com

Probably you can use style. 也许你可以使用风格。

Do not know exactly but one of those if changed in your theme should do the job. 不确切地知道,但如果你的主题发生了变化,其中一个应该完成这项工作。 Change background in styles to your custom drawables. 将样式背景更改为自定义drawable。

<style name="AppTheme">
    <item name="android:listPopupWindowStyle">@style/yourPopupWindowStyle</item>
    <item name="android:popupMenuStyle">@style/yourPopupWindowStyle</item>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM