LayoutInflater attachToRoot 参数是什么意思? [英] What does the LayoutInflater attachToRoot parameter mean?

查看:35
本文介绍了LayoutInflater attachToRoot 参数是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LayoutInflater.inflate 文档对我来说并不清楚 attachToRoot 参数的用途.

attachToRoot:膨胀的层次结构是否应该附加到根参数上?如果为 false,则 root 仅用于创建正确的XML 中根视图的 LayoutParams 子类.

attachToRoot: whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

有人可以更详细地解释一下,特别是根视图是什么,也许可以展示一个 truefalse 值之间行为变化的例子?>

Could someone please explain in more detail, specifically what the root view is, and maybe show an example of a change in behavior between true and false values?

推荐答案

现在或现在

第三个"参数 attachToRoot 是真还是假的主要区别是这个.

NOW OR NOT NOW

The main difference between the "third" parameter attachToRoot being true or false is this.

当你放置 attachToRoot

When you put attachToRoot

true :将子视图添加到父视图立即
false:将子视图添加到父视图现在.
稍后再添加.`

true : add the child view to parent RIGHT NOW
false: add the child view to parent NOT NOW.
Add it later. `

什么时候以后?

稍后是当您使用例如 parent.addView(childView)

That later is when you use for eg parent.addView(childView)

一个常见的误解是,如果 attachToRoot 参数为 false,那么子视图将不会添加到父视图.错误
在这两种情况下,子视图都会添加到父视图中.这只是时间的问题.

A common misconception is, if attachToRoot parameter is false then the child view will not be added to parent. WRONG
In both cases, child view will be added to parentView. It is just the matter of time.

inflater.inflate(child,parent,false);
parent.addView(child);   

等价于

inflater.inflate(child,parent,true);

一个很大的禁忌
当您不负责将子视图添加到父视图时,您永远不应该将 attachToRoot 传递为 true.
例如添加片段时

A BIG NO-NO
You should never pass attachToRoot as true when you are not responsible for adding the child view to parent.
Eg When adding Fragment

public View onCreateView(LayoutInflater inflater,ViewGroup parent,Bundle bundle)
  {
        super.onCreateView(inflater,parent,bundle);
        View view = inflater.inflate(R.layout.image_fragment,parent,false);
        .....
        return view;
  }

如果你传递第三个参数为真,你会因为这个人得到 IllegalStateException.

if you pass third parameter as true you will get IllegalStateException because of this guy.

getSupportFragmentManager()
      .beginTransaction()
      .add(parent, childFragment)
      .commit();

因为您已经错误地在 onCreateView() 中添加了子片段.调用 add 会告诉你子视图已经添加到父视图因此 IllegalStateException.
这里你不负责添加childView,FragmentManager负责.所以在这种情况下总是传递 false .

Since you have already added the child fragment in onCreateView() by mistake. Calling add will tell you that child view is already added to parent Hence IllegalStateException.
Here you are not responsible for adding childView, FragmentManager is responsible. So always pass false in this case.

注意: 我还读到,如果 attachToRoot 为 false,parentView 将不会获得 childView touchEvents.不过我还没有测试过.

NOTE: I have also read that parentView will not get childView touchEvents if attachToRoot is false. But I have not tested it though.

这篇关于LayoutInflater attachToRoot 参数是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆