Java 什么';这是布局更平坦和更平坦之间的区别;s工厂和工厂2
有两个公共接口:Java 什么';这是布局更平坦和更平坦之间的区别;s工厂和工厂2,java,android,layout-inflater,Java,Android,Layout Inflater,有两个公共接口: 在android sdk中,但官方文档甚至文档都无法提供有关此接口的有用信息 从消息来源我了解到,如果设置了Factory2,则将使用它,否则将使用Factory: View view; if (mFactory2 != null) { view = mFactory2.onCreateView(parent, name, context, attrs); } else if (mFactory != null) { view = mFactory.onCrea
在android sdk中,但官方文档甚至文档都无法提供有关此接口的有用信息 从消息来源我了解到,如果设置了
Factory2
,则将使用它,否则将使用Factory
:
View view;
if (mFactory2 != null) {
view = mFactory2.onCreateView(parent, name, context, attrs);
} else if (mFactory != null) {
view = mFactory.onCreateView(name, context, attrs);
} else {
view = null;
}
setFactory2()
还有非常简洁的文档:
/**
* Like {@link #setFactory}, but allows you to set a {@link Factory2}
* interface.
*/
public void setFactory2(Factory2 factory) {
如果要将自定义工厂设置为
LayoutInflater
,应使用哪个工厂?
它们的区别是什么?唯一的区别是,在
Factory2
中,您可以配置新视图的父视图将是谁。
用法-
当您需要为新视图设置特定父视图时,请使用Factory2
正在创建。(仅支持API 11及以上版本)
代码-LayoutFlater源代码:(删除不相关的代码后)
现在工厂2
:
public interface Factory2 extends Factory {
// @param parent The parent that the created view will be placed in.
// @return View Newly created view.
public View onCreateView(View parent, String name, Context context, AttributeSet attrs);
}
现在您可以看到Factory2
只是使用View parent
选项重载Factory
如果要将自定义工厂设置为,应使用哪个工厂
更平坦?它们之间有什么区别
如果需要提供创建的视图将放置在其中的父级,则需要使用Factory2
。但如果您的目标API级别为11+,则通常使用Factory2
。否则,只需使用工厂
这里是工厂
class MyLayoutInflaterFactory implements LayoutInflater.Factory {
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
if (TextUtils.equals(name, "MyCustomLayout")) {
return new MyCustomLayout(context, attrs);
}
// and so on...
return super.onCreateView(name, context attrs);
}
}
这里是Factory2
class MyLayoutInflaterFactory2 implements LayoutInflater.Factory2 {
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
if (TextUtils.equals(name, "MyCustomLayout")) {
return new MyCustomLayout(context, attrs);
}
// and so on...
return super.onCreateView(parent, name, context, attrs);
}
}
class MyLayoutInflaterFactory2 implements LayoutInflater.Factory2 {
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
if (TextUtils.equals(name, "MyCustomLayout")) {
return new MyCustomLayout(context, attrs);
}
// and so on...
return super.onCreateView(parent, name, context, attrs);
}
}