search

Home  >  Q&A  >  body text

android - LayoutInflater.inflater的问题?

新手,在看inflater的时候有点疑问
在LayoutInflater.inflater(int resId,ViewGroup root,boolean attchtoroot)这个方法中,
其中第三个参数是什么意思,什么时候用true,什么时候用false?

黄舟黄舟2772 days ago446

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 17:38:02

    To put it simply, if each View requires a LayoutParams to determine its layout position, the layout prefix configuration we make in the Layout resource file will basically be recorded in LayoutParams. But LayoutParams are not generated based on the current View, but defined based on the parent View. For example, if a TextView exists in a LinearLayout, then it has LayoutParams based on LinearLayout.LayoutParams, because in this way the TextView will have some layout attributes unique to LinearLayout.
    That is exactly the case. When we use LayoutInflater.inflater to load View from the resource file, the top-level View does not have a parent View. At this time, the layout series parameters we set in this View may apply. Lost due to the adoption of the most basic LayoutParams. In order to ensure that parameters are not lost, we can provide root, which is a reference to the parent View, indicating that the LayoutParams we use are consistent with the LayoutParams provided by the root class. Under normal circumstances, we will give the parent View object that the loaded View will join, so that consistency is easier to ensure. root,它就是一个父View的参考,表示我们使用的LayoutParams与root类所提供的LayoutParams一致。在通常情况下,我们就会给出载入的View即将加入的父View对象,这样一致性更容易保证。
    而第三项attchToRoot其实就是一个快捷操作,表示在使用root作为参考时,也同时把View加入到root里,成为root的子View。在Android默认的重载中,我们提供root且不为空时,attchToRoot的值就是true,表示默认就把View加入到了root里。
    虽然root和true是Android默认采用的方式,但通常我们不这么使用,因为LayoutInflater.inflater有个很特殊的返回方式,就是当我们传入的attchToRoot为true时,LayoutInflater.inflater会返回root而不是被载入的View本身。而在通常情况下,我们载入View都希望对View进行一些赋值等操作,也就是说我们更希望直接得到View本身。所以通常习惯的搭配是root配false这种方式,在对View赋值完成之后,在加入到rootThe third item attchToRoot is actually a shortcut operation, which means that when using root as a reference, the View will also be added to root, becomes a sub-View of root. In Android's default overload, when we provide root and it is not empty, the value of attchToRoot is true, which means that the View is added to root inside.

    Although root and true are the default methods used by Android, we usually do not use them this way because LayoutInflater.inflater has a very special return method, that is, when we pass in the When attchToRoot is true, LayoutInflater.inflater will return root instead of the loaded View itself. Under normal circumstances, when we load a View, we hope to perform some assignment operations on the View, which means we prefer to get the View itself directly. Therefore, the usual combination is root with false. After the View assignment is completed, it is added to root. #🎜🎜#

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 17:38:02

    @有明’s answer couldn’t be better.
    If the poster needs to see more content, please read the official document

    reply
    0
  • Cancelreply