更改Android菜单的背景颜色[重复
-
01-10-2019 - |
题
这个问题在这里已经有一个答案:
我正在尝试将标准浅灰色更改为浅绿色。似乎没有一种简单的方法来执行此操作(例如,通过Android主题),但我发现了此页面上解释的解决方法: http://tinyurl.com/342dgn3.
作者似乎消失了,有人可以帮助我整合此代码吗?我不明白我需要在哪里实施 LayoutInflater
工厂课程。
解决方案
当ur膨胀时,菜单调用此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;
}});
}
其他提示
显然,这是许多程序员遇到的问题,而Google尚未提供令人满意的,支持的解决方案。
Abhay Kumar和Nik Reiman发布的SetMenubackground()hack是一个很好的开始,但它要么崩溃,要么在Android 2.3上不起作用。
请在此Stackoverflow问题中查看我的答案(Louis Semprini),以便在2.1、2.2和2.3上有效评论,更精致的黑客(尽管我们永远无法保证这一点):
另外,这里还有许多其他资源,您可能会发现这个问题:
http://www.macadamian.com/blog/post/android_-_theming_the_unthemable/
http://www.codeproject.com/kb/android/androidmenusmyway.aspx
在OnCreate中使用SetMenubackground。
不隶属于 StackOverflow