更改Android菜单的背景颜色
我试图将标准浅灰色更改为浅绿色。似乎没有简单的方法来做到这一点(例如通过Android主题),但我找到了一个解决方法,如本页所述:http://tinyurl.com/342dgn3。更改Android菜单的背景颜色
作者似乎消失了,有人可以帮我整合这段代码吗?我不明白在哪里需要实施LayoutInflater
工厂类。
当乌尔都充气菜单调用此setMenuBackground()方法
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu,menu);
setMenuBackground();
return true;
}
,并在setMenuBackground()方法
protected void setMenuBackground(){
// Log.d(TAG, "Enterting setMenuBackGround");
getLayoutInflater().setFactory(new Factory() {
public View onCreateView(String name, Context context, AttributeSet attrs) {
if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
try { // Ask our inflater to create the view
LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attrs);
/* The background gets refreshed each time a new item is added the options menu.
* So each time Android applies the default background we need to set our own
* background. This is done using a thread giving the background change as runnable
* object */
new Handler().post(new Runnable() {
public void run() {
// sets the background color
view.setBackgroundResource(R.color.androidcolor);
// sets the text color
((TextView) view).setTextColor(Color.BLACK);
// sets the text size
((TextView) view).setTextSize(18);
}
});
return view;
}
catch (InflateException e) {}
catch (ClassNotFoundException e) {}
}
return null;
}});
}
有没有人得到这个工作?似乎没有效果(姜饼) – 2012-05-31 18:42:54
它与Android 2.2的工作没有检查2.3 – 2012-06-01 03:55:18
它不工作2.1这里 – melanke 2012-07-16 21:58:34
使用onCreate中的setMenuBackground。
哎呀,我看到你已经回答了你自己的问题。不错的工作。 – locoboy 2011-03-19 21:09:08
哪里?哪一个OnCreate – user1882196 2015-07-28 08:17:53
这写这显然是很多程序员有问题Google尚未提供令人满意的支持解决方案。
Abhay Kumar和Nik Reiman发布的setMenuBackground()黑客入门是一个好的开始,但它在Android 2.3上崩溃或不起作用。
请参阅我的回答(Louis Semprini)在这个*问题,以获得更好的评论和更精致的破解工作在2.1,2.2和2.3(并且应该对3.X没有伤害,虽然我们不能保证此):
How to change the background color of the options menu?
而且,这里有您可能会发现有助于这个问题的许多其他资源:
Change background color of android menu
Android: customize application's menu (e.g background color)
http://www.macadamian.com/blog/post/android_-_theming_the_unthemable/
Android MenuItem Toggle Button
Is it possible to make the Android options menu background non-translucent?
http://www.codeproject.com/KB/android/AndroidMenusMyWay.aspx
如果有人有兴趣我已经解决了。 http://pastebin.com/1QHGTMUW 只需调用onCreate中的setMenuBackground – rciovati 2010-04-27 21:11:12
以下是我在Android 4.0+上获得自定义Action Bar菜单背景色的方法:http://*.com/a/20077381/56285 – Jonik 2013-11-19 17:10:14
我试试你编码,但它首先告诉我删除@Override,然后没有hapes。我试图改变菜单背景颜色。 – Kenji 2014-03-15 14:53:43