Activity布局创建

一个Activity在setContentView后创建的布局以及布局在Activity内的层级如下:

setContentView整个过程

1. 创建DecorView,DecorView实际是一个FrameLayout;

2. 依据theme加载不同的窗口修饰布局文件;

3. 加载setContentView指定的布局,并且把它添加到窗口修饰布局文件中id为content的FrameLayout里;

获取LayoutInflater的两种方式

    LayoutInflater lif = LayoutInflater.from(Context context);

    LayoutInflater lif = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

可以看见from方法仅仅是对getSystemService的一个安全封装而已

    public static LayoutInflater from(Context context) {
        LayoutInflater LayoutInflater =
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (LayoutInflater == null) {
            throw new AssertionError("LayoutInflater not found.");
        }
        return LayoutInflater;
    }

resource,root,attachToRoot

  • root不为空时,才会给resource 根布局设置LayoutParams,否则根布局layout_width、layout_height不生效;

  • resource:布局文件id;root:要把xml布局添加到的根布局;attachToRoot:是否添加root作为父布局,为true时执行addView操作;

  • 无论root attachToRoot两个参数如何设置,都会设置resource里除根布局下面的子View 的LayoutParams。因为在xml文件里,只有根布局的父View是不确定的;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值