MenuItem的背景不会改变

时间:2012-01-10 09:05:15

标签: android background menuitem

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

<?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>

我在这里应用主题:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
   style="@style/Theme">
      <item android:id="@+id/add"/>......
</menu>

编辑:这是logcat的输出

01-10 05:47:08.434: E/AndroidRuntime(30489): FATAL EXCEPTION: main
01-10 05:47:08.434: E/AndroidRuntime(30489): android.content.res.Resources$NotFoundException: Resource ID #0x0
01-10 05:47:08.434: E/AndroidRuntime(30489):    at android.content.res.Resources.getValue(Resources.java:901)
01-10 05:47:08.434: E/AndroidRuntime(30489):    at android.content.res.Resources.getDrawable(Resources.java:589)
01-10 05:47:08.434: E/AndroidRuntime(30489):    at com.android.internal.policy.impl.PhoneWindow.openPanel(PhoneWindow.java:500)
01-10 05:47:08.434: E/AndroidRuntime(30489):    at com.android.internal.policy.impl.PhoneWindow.onKeyUpPanel(PhoneWindow.java:703)
01-10 05:47:08.434: E/AndroidRuntime(30489):    at com.android.internal.policy.impl.PhoneWindow.onKeyUp(PhoneWindow.java:1475)
01-10 05:47:08.434: E/AndroidRuntime(30489):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1845)
01-10 05:47:08.434: E/AndroidRuntime(30489):    at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2758)
01-10 05:47:08.434: E/AndroidRuntime(30489):    at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2730)
01-10 05:47:08.434: E/AndroidRuntime(30489):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1999)
01-10 05:47:08.434: E/AndroidRuntime(30489):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 05:47:08.434: E/AndroidRuntime(30489):    at android.os.Looper.loop(Looper.java:150)
01-10 05:47:08.434: E/AndroidRuntime(30489):    at android.app.ActivityThread.main(ActivityThread.java:4385)
01-10 05:47:08.434: E/AndroidRuntime(30489):    at java.lang.reflect.Method.invokeNative(Native Method)
01-10 05:47:08.434: E/AndroidRuntime(30489):    at java.lang.reflect.Method.invoke(Method.java:507)
01-10 05:47:08.434: E/AndroidRuntime(30489):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
01-10 05:47:08.434: E/AndroidRuntime(30489):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
01-10 05:47:08.434: E/AndroidRuntime(30489):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:3)

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

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

"Theme"中定义styles.xml时,在样式中添加父级,如下所示:

编辑:这对我来说是尝试改变颜色

<resources>
     <style name="Theme" parent="android:Theme">
        <item name="@android:panelFullBackground">#9C133C</item>
        <item name="@android:panelColorBackground">#9BCD08</item>
        <item name="@android:panelBackground">#9C133C</item>
     </style>
</resources>

答案 1 :(得分:2)

似乎android:panelBackground应该是可绘制的,而不是颜色。

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

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

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

答案 2 :(得分:0)

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

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);
    }