互換性ライブラリを有効にして、 "java.lang.illegalstateexception:このレイアウトプラットフォームで既にファクトリが設定されている"ことを避けるには、既に設定されているFactoryへの最終参照を取得し、onCreateViewを独自のFactoryで呼び出します。 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
実際に例外はスローされませんでしたが、テキストの色はまだグレーです:( –