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


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路宇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值