为了保持兼容性库的正常工作并避免“java.lang.illegalstateexception:已在此layoutinflater上设置了一个工厂”,您需要获取已设置的Factory的最终引用并在您的Factory中调用它的onCreateView。 onCreateView。在此之前,反省招必须要使用,让您可以设定一个更多的时间一个工厂到LayoutInflater:
LayoutInflater layoutInflater = getLayoutInflater();
final Factory existingFactory = layoutInflater.getFactory();
// use introspection to allow a new Factory to be set
try {
Field field = LayoutInflater.class.getDeclaredField("mFactorySet");
field.setAccessible(true);
field.setBoolean(layoutInflater, false);
getLayoutInflater().setFactory(new Factory() {
@Override
public View onCreateView(String name, final Context context, AttributeSet attrs) {
View view = null;
// if a factory was already set, we use the returned view
if (existingFactory != null) {
view = existingFactory.onCreateView(name, context, attrs);
}
// do whatever you want with the null or non-null view
// such as expanding 'IconMenuItemView' and changing its style
// or anything else...
// and return the view
return view;
}
});
} catch (NoSuchFieldException e) {
// ...
} catch (IllegalArgumentException e) {
// ...
} catch (IllegalAccessException e) {
// ...
}
不工作。抛出“android.view.InflateException:二进制XML文件行#17:错误膨胀类com.android.internal.view.menu.ActionMenuItemView” – MSIslam
实际上没有更多异常抛出,但我的文本颜色仍然是灰色:( –