爲了保持兼容性庫的正常工作並避免「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
實際上沒有更多異常拋出,但我的文本顏色仍然是灰色:( –