下面列出了android.view.LayoutInflater#setFactory2 ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static LayoutInflater inject(Context context) {
LayoutInflater inflater;
if (context instanceof Activity) {
inflater = ((Activity) context).getLayoutInflater();
} else {
inflater = LayoutInflater.from(context);
}
if (inflater == null) {
return null;
}
if (inflater.getFactory2() == null) {
BackgroundFactory factory = setDelegateFactory(context);
inflater.setFactory2(factory);
} else if (!(inflater.getFactory2() instanceof BackgroundFactory)) {
forceSetFactory2(inflater);
}
return inflater;
}
public static LayoutInflater inject(Context context) {
LayoutInflater inflater;
if (context instanceof Activity) {
inflater = ((Activity) context).getLayoutInflater();
} else {
inflater = LayoutInflater.from(context);
}
if (inflater == null) {
return null;
}
if (inflater.getFactory2() == null) {
BackgroundFactory factory = setDelegateFactory(context);
inflater.setFactory2(factory);
} else if (!(inflater.getFactory2() instanceof BackgroundFactory)) {
forceSetFactory2(inflater);
}
return inflater;
}
/**
* Attach a custom Factory interface for creating views while using
* this LayoutInflater. This must not be null, and can only be set once;
* after setting, you can not change the factory.
*
* @see LayoutInflater#setFactory(android.view.LayoutInflater.Factory)
* @deprecated Use {@link #setFactory2(LayoutInflater, LayoutInflater.Factory2)} instead to set
* and {@link LayoutInflater#getFactory2()} to get the factory.
*/
@Deprecated
public static void setFactory(
LayoutInflater inflater, LayoutInflaterFactory factory) {
if (Build.VERSION.SDK_INT >= 21) {
inflater.setFactory2(factory != null ? new LayoutInflaterCompat.Factory2Wrapper(factory) : null);
} else {
final LayoutInflater.Factory2 factory2 = factory != null
? new LayoutInflaterCompat.Factory2Wrapper(factory) : null;
inflater.setFactory2(factory2);
final LayoutInflater.Factory f = inflater.getFactory();
if (f instanceof LayoutInflater.Factory2) {
// The merged factory is now set to getFactory(), but not getFactory2() (pre-v21).
// We will now try and force set the merged factory to mFactory2
forceSetFactory2(inflater, (LayoutInflater.Factory2) f);
} else {
// Else, we will force set the original wrapped Factory2
forceSetFactory2(inflater, factory2);
}
}
}
/**
* Attach a custom {@link LayoutInflater.Factory2} for creating views while using
* this {@link LayoutInflater}. This must not be null, and can only be set once;
* after setting, you can not change the factory.
*
* @see LayoutInflater#setFactory2(android.view.LayoutInflater.Factory2)
*/
public static void setFactory2(
LayoutInflater inflater, LayoutInflater.Factory2 factory) {
inflater.setFactory2(factory);
if (Build.VERSION.SDK_INT < 21) {
final LayoutInflater.Factory f = inflater.getFactory();
if (f instanceof LayoutInflater.Factory2) {
// The merged factory is now set to getFactory(), but not getFactory2() (pre-v21).
// We will now try and force set the merged factory to mFactory2
forceSetFactory2(inflater, (LayoutInflater.Factory2) f);
} else {
// Else, we will force set the original wrapped Factory2
forceSetFactory2(inflater, factory);
}
}
}
static void setFactory(LayoutInflater inflater, PluginFactoryInterface factory) {
if (Build.VERSION.SDK_INT >=11) {
final LayoutInflater.Factory2 factory2 = factory != null
? new FactoryWrapper2(factory) : null;
inflater.setFactory2(factory2);
if (Build.VERSION.SDK_INT < 21) {
final LayoutInflater.Factory f = inflater.getFactory();
if (f instanceof LayoutInflater.Factory2) {
// The merged factory is now set to getFactory(), but not getFactory2() (pre-v21).
// We will now try and force set the merged factory to mFactory2
forceSetFactory2(inflater, (LayoutInflater.Factory2) f);
} else {
// Else, we will force set the original wrapped Factory2
forceSetFactory2(inflater, factory2);
}
}
} else {
final LayoutInflater.Factory factory1 = factory != null
? new FactoryWrapper(factory) : null;
inflater.setFactory(factory1);
}
}
/**
* Install Folivora's ViewFactory to current context. note that if
* you are using AppCompatActivity, this method should called after
* your activity's super.onCreate() method, since AppCompatDelegate
* will install a {@link LayoutInflater.Factory2} factory2 to this
* context.
*
* @param ctx context to enable folivora support
*/
public static void installViewFactory(Context ctx) {
LayoutInflater inflater = LayoutInflater.from(ctx);
LayoutInflater.Factory2 factory2 = inflater.getFactory2();
if (factory2 instanceof FolivoraViewFactory) return;
FolivoraViewFactory viewFactory = new FolivoraViewFactory();
viewFactory.mFactory2 = factory2;
if (factory2 != null) {
FolivoraViewFactory.forceSetFactory2(inflater, viewFactory);
} else {
inflater.setFactory2(viewFactory);
}
}
private static void installViewFactoryIfNeeded() {
Collection<BridgeContext> contexts = peekContexts();
for (BridgeContext context : contexts) {
LayoutInflater inflater = LayoutInflater.from(context);
if (inflater.getFactory2() == null) {
inflater.setFactory2(new ViewFactory(inflater, context.getLayoutlibCallback()));
}
}
}
private void initLayoutInflater() {
mLayoutInflater = (LayoutInflater) super.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (null != mLayoutInflater) {
mLayoutInflater.setFactory2(this);
}
}