我想更改菜单选项的背景颜色。我得到错误:
FATAL EXCEPTION: main
java.lang.IllegalStateException: A factory has already been set on this LayoutInflater
at android.view.LayoutInflater.setFactory(LayoutInflater.java:277)
我使用这段代码:
private void setMenuBackground() {
getLayoutInflater().setFactory(new Factory() {
@Override
public View onCreateView (String name, Context context, AttributeSet attrs) {
if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
try {
LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attrs);
new Handler().post( new Runnable() {
public void run () {
view.setBackgroundColor(Color.GRAY);
}
});
return view;
}
catch (InflateException e) {
}
catch (ClassNotFoundException e) {
}
}
return null;
}
});
}
我找到了一些答案,但它们对我没有帮助。
我该如何解决这个问题?谢谢。