一、LayoutInflater.inflate()
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);