问题
I posted a similar question regarding my code compiling but I managed to get everything running. However, my code doesn't change the default white background to a darker color. Here is my code:
<?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>
I'm applying the theme here:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Theme">
<item android:id="@+id/add"/>......
</menu>
Edit: Here is the output from the 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)
回答1:
Instead of setting that theme to MenuItem
, try setting this theme to your Application
tag in AndroidManifest.xml
like this:
...
<application android:theme="@style/Theme">
...
Add a parent in style when defining "Theme"
in styles.xml
like this:
Edit: this is working for me try to change colors
<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>
回答2:
It appears that android:panelBackground
should be a drawable, not a color.
A good place to start would probably be locating Android's default nine-patch in the SDK's platform resources (menu_hardkey_panel_holo_dark.9.png
), copying them to your own drawable folders and editing them. Finally, as Adil pointed out, add the items to the style declaration as:
<style name="AppTheme" parent="Theme.Sherlock">
<item name="android:panelBackground">@drawable/menu_hardkey_panel</item>
</style>
(Note that here I'm using ActionBarSherlock, hence the parent theme.)
回答3:
Add this to the activity you want to change the menu appearance for:
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);
}
来源:https://stackoverflow.com/questions/8800736/background-of-menuitem-wont-change