LayoutInflater

  1. 获取LayoutInflater 对象 有三种方法
    LayoutInflater inflater=LayoutInflater.from(this);
    LayoutInflater inflater=getLayoutInflater();
    LayoutInflater inflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
  2. inflate方法 public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
        LayoutInflater inflater = LayoutInflater.from(this);
        inflater.inflate(R.layout.linearlayout, ll,true);
    }
    
 当root不为null,attachToRoo为true时候,会自动将第一个参数所指定的R.layout.linearlayout,添加到第二个参数所指定的view中,如果我在最后一行添加 II.addView(inflate)方法 则会报错
 java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
当 root不为null,attachToRoot为false,如果我在最后一行添加 II.addView(inflate)方法 可以运行,和正常一样。II.addView(inflate)方法,我们也可以在xml界面中直接添加
当root为null,attachToRoot无论为null或者不为null都一样,但是linearlayout II并没有处于某一个容器中,所以它的根节点的宽高属性会失效,这个时候不管我给linearlayout的根节点的宽高设置什么,都是没有效果的,它都是包裹button,如果我修改button,则button会立即有变化,因为button是处于某一个容器中的。



 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值