gpt4 book ai didi

android - 膨胀 subview 的两种方法之间有什么区别吗?

转载 作者:行者123 更新时间:2023-12-01 01:07:23 29 4
gpt4 key购买 nike

第一种方法:

LinearLayout parent = ...;
View child = LayoutInflator.inflate(context, parent, true);

第二种方法:
LinearLayout parent = ...;
View child = LayoutInflator.inflate(context, null, false);
parent.addView(child);

有什么区别吗?

最佳答案

如果您检查 inflate 方法的来源,您会发现:

if (root != null) {
if (DEBUG) {
System.out.println("Creating params from root: " +
root);
}

// Create layout params that match root, if supplied

params = root.generateLayoutParams(attrs);
if (!attachToRoot) {

// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
temp.setLayoutParams(params);
}
}

/* ... */

// We are supposed to attach all the views we found (int temp)
// to root. Do that now.

if (root != null && attachToRoot) {
root.addView(temp, params);
}

所以在你的例子中没有区别。

在这种情况下会有所不同
View child = LayoutInflator.inflate(context, parent, false);

一个 child 将拥有与 parent 相同的 LayoutParams 但它不会被附加,所以它只是单独的 View 。

关于android - 膨胀 subview 的两种方法之间有什么区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17689907/

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