互換性ライブラリの動作を維持し、「java.lang.illegalstateexception: このレイアウトインフレータにファクトリが既に設定されています」を回避するには、すでに設定されているファクトリへの最終参照を取得し、独自の Factory.onCreateView 内でその onCreateView を呼び出す必要があります。その前に、イントロスペクション トリックを使用して、もう一度 Factory を 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) {
// ...
}