View.inflate()和LayoutInflater.inflate()的区别

一、LayoutInflater.inflate()


该方法适用于所有布局填充的的场景,但使用前需要先实例化LayoutInflater对象

1. 获取LayoutInflater实例

  • getLayoutInflater();
    这个方法可以在Activity和Fragment中使用,不过在Fragment中使用时,要传入一个bundle参数

    // Activity中使用
    LayoutInflater layoutInflater = getLayoutInflater();
    // Fragment中使用
    LayoutInflater layoutInflater = getLayoutInflater(savedInstanceState);
    
  • getSystemService();
    这个为Context的方法,只要有上下文就能调用

    LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
    
  • LayoutInflater.from();
    这个是LayoutInflater的静态方法,传入上下文即可

    LayoutInflater inflater = LayoutInflater.from(this);
    

2. LayoutInflater.inflate()的重载

常用的是下面两种:

  • inflate(int resource,ViewGroup root,boolean attachToRoot)

  • resource:布局资源id

  • root:resource生成view对象要填入的父布局。为null,则返回的view就是布局资源;否则,需要参照第三个参数

  • attachToRoot:是否将resource生成view对象填入root,以root作为最终返回view的根布局。 false,root不为null,则提供root的LayoutParams约束resource生成的view;true,root不为null,以root作为最终返回view的根布局

  • inflate(int resource,ViewGroup root)

  • resource:布局资源

  • root:resource生成view对象要填入的父布局。为null,则返回的view就是布局资源的根布局;否则,返回的view以传入的root为根布局(相当于上面的那个第三个参数为true)

该方法,实际还是调用了上一个方法

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
    return inflate(resource, root, root != null);
}

二、 View.inflate()


这个是View类的静态方法,可直接调用,实际上还是使用了LayoutInflater,所以,它没有直接使用LayoutInflater.inflate()强大

public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {
    LayoutInflater factory = LayoutInflater.from(context);
    return factory.inflate(resource, root);
}

参数含义:

  • context:上下文
  • resource:布局资源
  • root:resource生成view对象要填入的父布局。为null,则返回resource生成view对象,否则将view填入到root中,以root作为根布局返回

三、总结


View.inflate()是封装了LayoutInflater的inflate()方法,由于是静态方法,比较简便;但LayoutInflater相当于比View多了一个三个参数的inflate()方法,功能更强大


个人总结,水平有限,如果有错误,希望大家能给留言指正!如果对您有所帮助,可以帮忙点个赞!如果转载,希望可以标明文章出处!最后,非常感谢您的阅读!

java.lang.IllegalStateException: ScrollView can host only one direct child at androidx.core.widget.NestedScrollView.addView(NestedScrollView.java:507) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1131) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72) at android.view.LayoutInflater.rInflate(LayoutInflater.java:1101) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1130) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72) at android.view.LayoutInflater.rInflate(LayoutInflater.java:1101) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088) at android.view.LayoutInflater.inflate(LayoutInflater.java:686) at android.view.LayoutInflater.inflate(LayoutInflater.java:505) at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:360) at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:443) at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:121) at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:722) at com.android.tools.idea.rendering.RenderTask.lambda$inflate$9(RenderTask.java:879) at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout$3.run(RenderExecutor.kt:194) at com.android.tools.idea.rendering.RenderExecutor$PriorityRunnable.run(RenderExecutor.kt:292) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:829)
06-06
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值