gpt4 book ai didi

java - LayoutInflater Factory 和 Factory 2 有什么区别

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:38:03 25 4
gpt4 key购买 nike

有两个公共(public)接口(interface):
LayoutInflater.FactoryLayoutInflater.Factory2在 android sdk 中,但官方文档无法说明有关此接口(interface)的有用信息,甚至 LayoutInflater文档。

从消息来源我了解到,如果设置了 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 中,您可以配置新 View 的 parent view 是谁。

用法 -
当您需要将特定父级设置为您的新 View 时,请使用 Factory2创建。(仅支持 API 11 及更高版本)

代码 - LayoutInflater 来源:(删除无关代码后)

public interface Factory {
// @return View Newly created view.
public View onCreateView(String name, Context context, AttributeSet attrs);
}

现在 Factory2:

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

关于java - LayoutInflater Factory 和 Factory 2 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39253009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com