改变MenuItem默认背景

@Override
public boolean onCreateOptionsMenu(Menu menu) {
setMenuBackground();
return super.onCreateOptionsMenu(menu);
}


protected void setMenuBackground() {
MenuDemo.this.getLayoutInflater().setFactory(
new android.view.LayoutInflater.Factory() {
@Override
public View onCreateView(String name, Context context,
AttributeSet attrs) {
// 指定自定义inflate的对象
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.upcorners_shape);



}
});
return view;
} catch (InflateException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return null;
}
});
}

 改变menu的颜色  在有些真机上试了,不行,不知道什么原因

你可能感兴趣的:(menu)