LayoutInflater两个inflate方法的区别

本文详细探讨了LayoutInflater的两个inflate方法的区别,特别是当root参数是否为null时的行为。重点讲解了如何通过设置root和attachToRoot参数来避免布局警告,并解释了在不同情况下,如何影响布局的加载和附加到父视图的过程。
摘要由CSDN通过智能技术生成

LayoutInflater类中有多个inflate方法,这里简要说一下:

public View inflate(int resource, ViewGroup root)

public View inflate(int resource, ViewGroup root, boolean attachToRoot)

这两个方法的区别,以及第二个方法中boolean变量的作用。

以前在使用ListView,Adapter的getView方法中,经常使用第一个方法来加载布局文件:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.list_item, null);
    }
}

这时IDE会弹出warning:

Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout’s root element)

提示我们不要传递”null”作为root的参数,因为需要决定root节点的属性。

在这里,可以换用另一个inflate方法来避免这个warning:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        // 使用三个参数的inflate方法,设定root参数为getView中的parent参数,设定attachToRoot为false
        con
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值