AndroidMenu背景图片,背景色的设置

项目中用到更改自定义AndroidMenu背景图片,背景色等,网上转载了一篇如下:本文转载自 池塘里的大象

更改背景颜色后效果如下:

具体代码如下:

public boolean onCreateOptionsMenu(Menu menu) {
		
		//MenuInflater mMenuInf = getMenuInflater();	
		//mMenuInf.inflate(R.menu.main_menu, menu);
		menu.add(0,R.id.main_menu_about,0,R.string.main_menu_about).setIcon(R.drawable.menu_icon_about);
		menu.add(0,R.id.main_menu_exit,1,R.string.main_menu_exit).setIcon(R.drawable.menu_icon_exit);
		SetMenubackgroudcolor();
		return true;
	}
public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
  case R.id.main_menu_about:

   break;

  case R.id.main_menu_exit://退出系統
   //取消通知
   //notificationManager.cancel(R.drawable.logo_icon_16);
   DialogUtil.Exitdialog(MyHomeActivity.this,notificationManager);
   break;
  }
  return true;
 }

更改颜色方法如下:

private void SetMenubackgroudcolor() {
		getLayoutInflater().setFactory(new Factory() {
			public View onCreateView(String name, Context context, AttributeSet attrs) {
				if(name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")
						|| name.equalsIgnoreCase("com.android.internal.view.menu.ListMenuItemView")){
					try {
						LayoutInflater f = getLayoutInflater();                           
						final View view = f.createView( name, null, attrs );                           
						new Handler().post( new Runnable() {                               
							public void run () {                                     
								//view.setBackgroundResource( R.drawable.menu_backg);//设置背景图片                                 
								view.setBackgroundColor(Color.parseColor("#F6F9FE"));//设置背景色                               
								}
							});
						 
					} catch (InflateException e) {
						e.printStackTrace();
					} catch (ClassNotFoundException e) {						 
						e.printStackTrace();
					}
				}
				return null;
			}
		});
	} 


就这样。。。不过本人没有测试!觉得应该是可行的!

版权声明:本文为iteye_3753原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/iteye_3753/article/details/82412614