Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.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 MenuItem won的背景';不变_Android_Background_Menuitem - Fatal编程技术网

Android MenuItem won的背景';不变

Android MenuItem won的背景';不变,android,background,menuitem,Android,Background,Menuitem,关于我的代码编译,我发布了一个类似的问题,但我设法让一切都运行起来。但是,我的代码不会将默认的白色背景更改为较暗的颜色。这是我的密码: <?xml version="1.0" encoding="utf-8"?> <resources> <style name="Theme"> <item name="@android:panelFullBackground">@android:color/background_dar

关于我的代码编译,我发布了一个类似的问题,但我设法让一切都运行起来。但是,我的代码不会将默认的白色背景更改为较暗的颜色。这是我的密码:

<?xml version="1.0" encoding="utf-8"?>
  <resources>
     <style name="Theme">
        <item name="@android:panelFullBackground">@android:color/background_dark</item>
        <item name="@android:panelColorBackground">@android:color/background_dark </item>
        <item name="@android:panelBackground">@android:color/background_dark</item>
     </style>
  </resources>

不要将该主题设置为
MenuItem
,而是尝试将该主题设置为
AndroidManifest.xml中的
应用程序
标记,如下所示:

...
<application android:theme="@style/Theme">
...

不要将该主题设置为
MenuItem
,而是尝试将该主题设置为
AndroidManifest.xml中的
应用程序
标记,如下所示:

...
<application android:theme="@style/Theme">
...

将此项添加到要更改其菜单外观的活动中:

protected final Factory menuFactory = new LayoutInflater.Factory() {
        @Override
        public View onCreateView(String name, final Context context, AttributeSet attrs) {
            if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
                try {
                    LayoutInflater f = LayoutInflater.from(context);
                    final View view = f.createView(name, null, attrs);
                    new Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            view.setBackgroundResource(android.R.color.background_dark);
                            // You could also change the text color like this:
                            ((TextView) view).setTextAppearance(context,  android.R.style.TextAppearance_Medium);
                        }
                    });
                    return view;
                } catch (Exception e) {
                }
            }
            return null;
        }
    };

@Override
protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        getLayoutInflater().setFactory(menuFactory);
    }

将此项添加到要更改其菜单外观的活动中:

protected final Factory menuFactory = new LayoutInflater.Factory() {
        @Override
        public View onCreateView(String name, final Context context, AttributeSet attrs) {
            if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
                try {
                    LayoutInflater f = LayoutInflater.from(context);
                    final View view = f.createView(name, null, attrs);
                    new Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            view.setBackgroundResource(android.R.color.background_dark);
                            // You could also change the text color like this:
                            ((TextView) view).setTextAppearance(context,  android.R.style.TextAppearance_Medium);
                        }
                    });
                    return view;
                } catch (Exception e) {
                }
            }
            return null;
        }
    };

@Override
protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        getLayoutInflater().setFactory(menuFactory);
    }

看起来,
android:panelBackground
应该是可绘制的,而不是颜色

一个好的起点可能是在SDK的平台资源中找到Android的默认九补丁(
菜单\u硬键\u面板\u holo\u dark.9.png
),将它们复制到您自己的可绘制文件夹中并进行编辑。最后,正如Adil所指出的,将项目添加到样式声明中,如下所示:

<style name="AppTheme" parent="Theme.Sherlock">
    <item name="android:panelBackground">@drawable/menu_hardkey_panel</item>
</style>

@可绘图/菜单\u硬键\u面板

(注意,这里我使用的是ActionBarSherlock,因此是父主题。)

看起来
android:panelBackground
应该是可绘制的,而不是颜色

一个好的起点可能是在SDK的平台资源中找到Android的默认九补丁(
菜单\u硬键\u面板\u holo\u dark.9.png
),将它们复制到您自己的可绘制文件夹中并进行编辑。最后,正如Adil所指出的,将项目添加到样式声明中,如下所示:

<style name="AppTheme" parent="Theme.Sherlock">
    <item name="android:panelBackground">@drawable/menu_hardkey_panel</item>
</style>

@可绘图/菜单\u硬键\u面板

(注意,这里我使用的是ActionBarSherlock,因此是父主题。)

你可以查看这篇文章。我已经查看了这些解决方案。其中许多包含bug,不适用于所有平台。请注意,您不需要在项目名称属性中使用“@”,只需要在项目值中使用“@”,因此,例如,它应该是@android:color/background\u darkyou可以查看此帖子。我已经检查了这些解决方案。其中许多包含bug,不适用于所有平台。请注意,您不需要在项目名称属性中使用“@”,只需要在项目值中使用“@”,因此,例如,它应该是@android:color/background\u dark运行您的代码会导致我的应用程序崩溃。为了澄清,我从定义菜单的xml文件中删除了style=“@style/Theme”,并将其放在应用程序标记中,而不是清单文件中。编辑我的项目构建目标是2.2你能发布日志吗。。!!我已经在问题中发布了logcat的输出运行您的代码会导致我的应用程序崩溃。为了澄清,我从定义菜单的xml文件中删除了style=“@style/Theme”,并将其放在应用程序标记中,而不是清单文件中。编辑我的项目构建目标是2.2你能发布日志吗。。!!我已经在我的问题中发布了logcat的输出