Android LayoutInflater.from(context).inflate()方法的作用

前些天发现了一个蛮有意思的人工智能学习网站,8个字形容一下"通俗易懂,风趣幽默",感觉非常有意思,忍不住分享一下给大家。
👉 点击跳转到教程

具体详情已经写在代码注释中

public class InputNumberView extends RelativeLayout {
    public InputNumberView(Context context) {
        this(context, null);
    }

    public InputNumberView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InputNumberView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //inflate方法的第一个参数:要载入的布局文件
        //第二个参数:ViewGroup,把布局文件应用到ViewGroup中,这里传入this即可
        //第三个参数:attachToRoot 填true表示,把这个载入的布局文件绑定到当前的ViewGroup里面
        //为true时,这样就会将载入的布局内容,自动的添加到ViewGroup中
        LayoutInflater.from(context).inflate(R.layout.view_number_view, this, true);

        //不填也行
        /*
          源码:不填默认就是true
          public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
                return inflate(resource, root, root != null);
            }

         */
        LayoutInflater.from(context).inflate(R.layout.view_number_view, this);

        //填false,需要手动添加载入的View
        View view = LayoutInflater.from(context).inflate(R.layout.view_number_view, this, false);
        addView(view);

        //以上三种方式功能都是一样的,都是把View添加到当前容器里。
    }


}

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>