gpt4 book ai didi

android - LayoutInflater.inflate 和 findViewById 的区别

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:37:15 27 4
gpt4 key购买 nike

像这样获取对小部件的引用有什么区别:

TableRow row = findViewById(R.id.table_row);

和:

TableRow row = (TableRow)LayoutInflater.from(this).inflate(R.layout.table_row, null);

TableRow 是其布局的根或者它只是布局的一小部分时,是否也有区别?

最佳答案

What is the difference...

第一个是在您的 Activity 中检索现有的小部件。

第二个是读取 XML 文件并创建小部件。第二个也有点问题,因为您很少想使用 LayoutInflater.from()(您通常在 Activity 上使用 getLayoutInflater() >),并且您很少想使用该 inflate() 变体(如果您不提供父容器,具有根 RelativeLayout 元素的布局资源将出现异常)。

Is there also a difference when the TableRow is a root of its layout or if its just a small part of a layout?

是的。区别与以前相同:检索现有小部件或创建新小部件。

关于android - LayoutInflater.inflate 和 findViewById 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28015060/

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