Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何在点击时更改选项菜单的颜色_Android - Fatal编程技术网

Android:如何在点击时更改选项菜单的颜色

Android:如何在点击时更改选项菜单的颜色,android,Android,默认情况下,当选择一个项目时,Android在选项菜单上显示橙色 如何在点击时将自己的颜色设置到选项菜单???android中的选项菜单可以定制为设置背景或更改文本外观。无法使用主题和样式更改菜单中的背景和文本颜色。android源代码data\res\layout\icon\u menu\u item\u layout.xml使用“com.android.internal.view.menu.iconmenuim”类的自定义项作为菜单布局。我们可以在上面的类中进行更改以自定义菜单。要实现同样的

默认情况下,当选择一个项目时,Android在选项菜单上显示橙色


如何在点击时将自己的颜色设置到选项菜单???

android中的选项菜单可以定制为设置背景或更改文本外观。无法使用主题和样式更改菜单中的背景和文本颜色。android源代码data\res\layout\icon\u menu\u item\u layout.xml使用“com.android.internal.view.menu.iconmenuim”类的自定义项作为菜单布局。我们可以在上面的类中进行更改以自定义菜单。要实现同样的效果,请使用LayoutInflater factory类并为视图设置背景和文本颜色

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    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() {
                            // set the background drawable
                            view.setBackgroundColor(any color);

                        }
                    });
                    return view;
                } catch (InflateException e) {
                    } catch (ClassNotFoundException e) {}
            }
            return null;
        }
    });
    return super.onCreateOptionsMenu(menu);
}
在您的drawable文件夹中创建一个名为selector.xml的xml文件,然后将其填充为

基本上,你是在告诉Android,当一个事件发生时,你想做一些事情,比如用户点击一个按钮或聚焦它等等

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/blue" /> <!-- focused -->
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/blue" /> <!-- focused and pressed-->
    <item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed -->
    <item android:drawable="@color/white" /> <!-- default -->
 </selector>
然后将菜单中使用的项目图标设置为

android:icon=@drawable/selector


有关使用StateListDrawable的更多信息,请参阅。

我已经在其他帖子中看到了这一点。没有任何简单的解决方案来设置颜色吗?直到现在,我都没有得到任何简单的解决方案。我只是按照上面的代码做的,我想这会改变选项菜单的默认颜色。但是我不想要这个。我想要的是,它应该只在单击时更改颜色。这只会更改选项菜单的默认颜色。你想在单击时更改它吗?这是我在问题中提出的问题。android中的颜色:drawable=@color/blue应该在color.xml文件中的值下定义。实际上,菜单项没有属性background。使用android:panelFullBackgroundsee我编辑了代码,直接更改android:icon属性,它对我有效。See我仅通过这个android:icon属性设置图标图像。如果我使用选择器并设置颜色,它将更改背景颜色,但无法设置图标图像。我想你明白了。