繁体   English   中英

Android:选择后如何更改选项菜单的背景颜色?

[英]Android:How to change the background color of the options menu when selected?

我在应用程序中创建了一个选项菜单。 我想更改所选菜单项的背景颜色。 请帮我这样做。

您必须尝试使用​​此代码,它将随着菜单文本更改菜单颜色。

 private void setMenuBackground() {
    // TODO Auto-generated method stub
     getLayoutInflater().setFactory(new Factory() {
            @Override
            public View onCreateView (String name, Context context, AttributeSet attrs) {
                if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
                try {

                        LayoutInflater f = getLayoutInflater();
                        final View view = f.createView(name, null, attrs);

                        new Handler().post( new Runnable() {
                            public void run () {
                                view.setBackgroundColor(Color.GRAY);
                            }
                        });
                        return view;
                    }
                    catch (InflateException e) {
                    }
                    catch (ClassNotFoundException e) {
                    }
                }
                return null;
            }
        });
}

暂无
暂无

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

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