如有可能,有什么方法可以定制手机菜单(由“MENU”按钮触发的那个)?我特别感兴趣的是:
- 将背景颜色从标准的浅灰色更改为深灰色
- 菜单项的对齐方式。我有4个项目,它们被自动分成2x2的形式,但我希望它们都在一行上(1x4)。
如有可能,有什么方法可以定制手机菜单(由“MENU”按钮触发的那个)?我特别感兴趣的是:
http://www.codeproject.com/KB/android/AndroidMenusMyWay.aspx
使用内置的菜单框架无法实现。
您可以拦截MENU按钮(通过onKeyDown()
等方式)并呈现您想要的内容,但请记住,用户将期望其外观与设备上的其他菜单相同。
使用样式。 这在我的 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>
如果可绘制的是一个普通的选择器,则...
<?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>
<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();
}