![](/img/trans.png)
[英]Android: How to trigger screen UI events (e.g incoming call screen) by a long running background service?
[英]Android: customize application's menu (e.g background color)
有什么方法(如果有办法)自定义菜单(由手机的MENU按钮触发的菜单)。 我对两件事特别感兴趣:
我创建了自己的菜单类。 它可能不是你想要的,但它应该有希望让你开始。 这是我发布的文章和源代码的可下载链接。
http://www.codeproject.com/KB/android/AndroidMenusMyWay.aspx
没有内置的菜单框架。
欢迎您截取MENU按钮(通过onKeyDown()
或其他东西)并渲染您想要的内容,但请记住,用户希望它看起来像其他菜单在他们的设备上。
您还可以实现“onCreateOptionsMenu”方法,该方法通常用于显示标准菜单,并在这种情况下显示您想要的任何内容。
在我的游戏中,当按下菜单按钮时,我实现了它以显示“Game Paused”对话框...
使用样式。 这适用于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>
...然后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>
这个答案有效但在使用ActionBarSherlock时崩溃了。 尽管如此,这仍然是一个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.