美文网首页
LayoutInflater中inflate()参数解析

LayoutInflater中inflate()参数解析

作者: dashingqi | 来源:发表于2020-09-08 00:01 被阅读0次
Android_Banner.jpg

简介

  • LayoutInflater中的inflate方法是将我们传递的xml文件通过IO操作,转换成View,这样我们就能得到View对象了。
  • 关于inflate方法的入参,请看下面代码
inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
  • 参数一,通常就是我们要转换成View对象的布局Id,参数二是父布局,参数三表示是否要将 resource添加到父布局中的
此次我们通过如下几种情况来分析 inflate的过程
情况一:root != null , attachToRoot为true
情况二:root!=null, attachToRoot为false
情况三: root==null,attachToRoot 是啥都行

解析

  • 我们有如下两个布局
// activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
</androidx.constraintlayout.widget.ConstraintLayout>

//inflate_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="500dp"
    android:background="#ff0000">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="this is inflater"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
情况一
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val view = LayoutInflater.from(this).inflate(R.layout.inflate_layout,container,true)
    }
}
  • 以上代码,就是将我们的inflate_layout布局添加到container布局中,实现效果如下


    infalte_true.jpg
  • 在这里我们没有使用container的addView方法将获取到的View添加到布局中,这是因为如果attachToRoot为itrue,在inflate的源码中又做addView的操作,如果你将获取的View再次添加会报错。
情况二
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val view = LayoutInflater.from(this).inflate(R.layout.inflate_layout,container,false)
    }
}
  • 以上代码通过inflate方法获取到布局inflate_layout的View对象,但是没有添加到container中,所以效果就是保持container原状,效果如下


    inflate_false.jpg
  • 此时我们是获取到了对应布局的View对象,但是没有add的操作,如果此时我们手动进行add操作
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val view = LayoutInflater.from(this).inflate(R.layout.inflate_layout,container,false)
        //将获取到View对象,添加到container布局中
        container.addView(view)
    }
}
  • 效果如下


    infalte_false.jpg
情况三
  • attachToRoot为false
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val view = LayoutInflater.from(this).inflate(R.layout.inflate_layout,null,false)
    }
}
  • 效果如下


    inflate_false.jpg
  • attachToRoot为true

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val view = LayoutInflater.from(this).inflate(R.layout.inflate_layout,null,true)
    }
}
  • 效果如下


    inflate_false.jpg
  • 很好解析上述现象,当Root为null,false就不用说了,本身就不添加到父布局中,为true,虽然一心想要添加到父布局中,可偏偏传递的父布局为null,所以添加失败了。‘

  • 当两者都手动执行add操作

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val view = LayoutInflater.from(this).inflate(R.layout.inflate_layout,null,true)
        container.addView(view)
    }
}
  • 效果如下


    infalte_root_null.jpg
  • 我们发现它的宽度没有match_parent,高度也没有500dp,现象是 wrap_content的布局
  • 其实很好解释的,我们看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);
                        }
                    }
  • 当root不为null,会获取到Root的LayoutParams,当 attachToRoot为false,会设置给resource布局对应的View,在这里我们的root为null嘛,所以获取到的View的layoutParams是不存在的。
  • 在看addView的源码
public void addView(View child) {
        addView(child, -1);
    }
 public void addView(View child, int index) {
        if (child == null) {
            throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
        }
        //分析1
        LayoutParams params = child.getLayoutParams();
        if (params == null) {
            params = generateDefaultLayoutParams();
            if (params == null) {
                throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
            }
        }
        addView(child, index, params);
    }
  • 分析1 当我们获取View的LayoutParams为null的时候,会调用generateDefaultLayoutParams()
protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }
  • 哈哈,看到这里真相大白了吧,所以就是wrap_content的布局方式
LayoutParams
  • LayoutParams 的作用是 子View用来告诉父控件,它想怎么放置。
  • 上述LayoutParams为null,说明子View不知道怎么放置,这时候ViewGroup就给了一个默认的LayoutParams ----> wrap_content

问题

ViewPager左右滑动,加载的Fragment 在onCreateView中,我们应该怎么使用inflate方法
  • 针对这种情况我们应该使用情况二这种方式去获取到View对象,也就是不将布局文件获取的View添加到ViewPager中
  • 因为,在ViewPager中会进行add的操作,此时如果attachToRoot为true,说明实现添加在Root中了,此时会报错。
    如果对LayoutInflater的inflate方法不熟悉的话,可以看我之前对infalte的分析

相关文章

网友评论

      本文标题:LayoutInflater中inflate()参数解析

      本文链接:https://www.haomeiwen.com/subject/etrrektx.html