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

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

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

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添加到当前容器里。
    }
}


目录
相关文章
|
6天前
|
Android开发
Android调用相机与相册的方法1
Android调用相机与相册的方法
39 0
|
6天前
|
Android开发 Python
Python封装ADB获取Android设备wifi地址的方法
Python封装ADB获取Android设备wifi地址的方法
66 0
|
6天前
|
机器学习/深度学习 Java Shell
[RK3568][Android12.0]--- 系统自带预置第三方APK方法
[RK3568][Android12.0]--- 系统自带预置第三方APK方法
49 0
|
5天前
|
Android开发
Android获取蓝牙设备列表的方法
Android获取蓝牙设备列表的方法
14 5
|
5天前
|
Android开发
Android获取当前系统日期和时间的三种方法
Android获取当前系统日期和时间的三种方法
16 4
|
6天前
|
API 开发工具 Android开发
调用Android原生@SystemApi、@Hide方法
调用Android原生@SystemApi、@Hide方法
9 1
|
6天前
|
程序员 Android开发
Android亮度调节的几种实现方法
Android亮度调节的几种实现方法
11 0
|
6天前
|
Shell Android开发
Android Activity重写dump方法实现通过adb调试代码
Android Activity重写dump方法实现通过adb调试代码
17 0
|
6天前
|
Android开发
Android APP 隐藏系统软键盘的方法
Android APP 隐藏系统软键盘的方法
38 0
|
6天前
|
Android开发
Android中去掉ActionBar的几种方法
Android中去掉ActionBar的几种方法
14 0