[英]Android: customize application's menu (e.g background color)
What is the way (if there is a way) to customize the menu (the one triggered by the MENU button of the phone). 有什么方法(如果有办法)自定义菜单(由手机的MENU按钮触发的菜单)。 I'm especially interested in two things:
我对两件事特别感兴趣:
I created my own menu class. 我创建了自己的菜单类。 It maybe isn't exactly what you want but it should hopefully get you started.
它可能不是你想要的,但它应该有希望让你开始。 Here is the article I published and a downloadable link to the source code.
这是我发布的文章和源代码的可下载链接。
http://www.codeproject.com/KB/android/AndroidMenusMyWay.aspx http://www.codeproject.com/KB/android/AndroidMenusMyWay.aspx
Not with the built-in menu framework. 没有内置的菜单框架。
You are welcome to intercept the MENU button (via onKeyDown()
or something) and render what you want, but bear in mind that users will be expecting it to look like the rest of the menus do on their device. 欢迎您截取MENU按钮(通过
onKeyDown()
或其他东西)并渲染您想要的内容,但请记住,用户希望它看起来像其他菜单在他们的设备上。
You can also just implement the "onCreateOptionsMenu" method, that is usually used to display the standard menu, and display whatever you want in this case. 您还可以实现“onCreateOptionsMenu”方法,该方法通常用于显示标准菜单,并在这种情况下显示您想要的任何内容。
In my game, I implemented it to display a "Game Paused" dialog box when the menu button is pressed... 在我的游戏中,当按下菜单按钮时,我实现了它以显示“Game Paused”对话框...
Use styles. 使用样式。 This works for me on Android 5.0
这适用于Android 5.0
<style name="AppTheme" parent="android:Theme.Material.Light">
<item name="android:colorPrimary">@color/primary</item>
<item name="android:actionOverflowMenuStyle">@style/PopupMenu.MyStyle</item>
</style>
<style name="PopupMenu.MyStyle" parent="android:Widget.PopupMenu">
<item name="android:popupBackground">@drawable/actionbar_item_background</item>
</style>
... then the drawable is a regular selector ...然后drawable是一个常规选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/primary"/>
<item android:drawable="@color/highlighted" android:state_pressed="true"/>
</selector>
主题中style.xml中的背景菜单颜色
<item name="android:panelFullBackground">@android:color/darker_gray</item>
This answer works but crashed for me when using ActionBarSherlock. 这个答案有效但在使用ActionBarSherlock时崩溃了。 Here's a hacky workaround to make this work nontheless.
尽管如此,这仍然是一个hacky解决方法。
// Black Vodoo! Do not try this at home.
final LayoutInflater li = getLayoutInflater();
final Class<LayoutInflater> clazz = LayoutInflater.class;
try {
final Field fieldSet = clazz.getDeclaredField("mFactorySet");
fieldSet.setAccessible(true);
fieldSet.setBoolean(li, false);
li.setFactory(new Factory() {
@Override
public View onCreateView(final String name,
final Context context, final AttributeSet attrs) {
if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
try {
final LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attrs);
new Handler().post(new Runnable() {
@Override
public void run() {
// Set the text color
((TextView) view).setTextColor(Color.WHITE);
}
});
return view;
} catch (final Exception e) {
}
}
return null;
}
});
} catch (final Exception e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.