更改android菜单的背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2719173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change background color of android menu
提问by rciovati
I'm trying to change the standard light grey to a light green. Seems that there is not a simple way to do this (through Android Themes, for example) but I have found a workaround as explained at this page: http://tinyurl.com/342dgn3.
我正在尝试将标准浅灰色更改为浅绿色。似乎没有一种简单的方法可以做到这一点(例如,通过 Android 主题),但我找到了一种解决方法,如本页所述:http: //tinyurl.com/342dgn3。
The author seems disappeared, can someone help me integrating this code? I don't understand where I need to implement the LayoutInflater
factory class.
作者似乎消失了,有人可以帮我整合这段代码吗?我不明白我需要在哪里实现LayoutInflater
工厂类。
采纳答案by Abhay Kumar
When ur are inflating the menu call this setMenuBackground() method
当你膨胀菜单时调用这个 setMenuBackground() 方法
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu,menu);
setMenuBackground();
return true;
}
and write this in the setMenuBackground() method
并将其写入 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;
}});
}
回答by Louis Semprini
This is clearly a problem that a lot of programmers have and to which Google has yet to provide a satisfactory, supported solution.
这显然是许多程序员都遇到的问题,Google 尚未提供令人满意的、受支持的解决方案。
The setMenuBackground() hack which Abhay Kumar and Nik Reiman posted is a good start but it either crashes or does not work on Android 2.3.
Abhay Kumar 和 Nik Reiman 发布的 setMenuBackground() hack 是一个好的开始,但它要么崩溃,要么在 Android 2.3 上不起作用。
Please see my answer (Louis Semprini) in this stackoverflow question for a better commented and more refined hack that works on 2.1, 2.2, and 2.3 (and that should do no harm on 3.X, though we can never guarantee this):
请在这个 stackoverflow 问题中查看我的回答 (Louis Semprini),以获得更好的评论和更精致的 hack,它适用于 2.1、2.2 和 2.3(这应该不会对 3.X 造成伤害,尽管我们永远不能保证这一点):
How to change the background color of the options menu?
Also, here are many other resources you may find helpful for this question:
此外,这里还有许多其他资源可能对您有帮助:
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/
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://www.codeproject.com/KB/android/AndroidMenusMyWay.aspx
回答by locoboy
Use the setMenuBackground in onCreate.
在 onCreate 中使用 setMenuBackground。