Android技巧小结之inflate布局文件

Android API提供了几种方式inflate一个xml布局文件。如下:

1、View类的静态方法inflate(Context context, int layoutId, View root)

2、Activity对象的getLayoutInflater()方法可以得到一个LayoutInflater对象,再调用该对象的inflate(int layoutId, View root)方法

示例:MainActivity.this.getLayoutInflater().inflate(R.layout.activity_main, null);

3、LayoutInflater类的静态方法from(Context context)返回一个LayoutInflater对象,再调用该对象的inflate(int layoutId, View root)方法

示例:LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main, null);

4、通过Context对象的getSystemService(Context.LAYOUT_INFLATER_SERVICE)得到一个LayoutInflater对象,再调用inflate()方法

示例:LayoutInflater inflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

inflater.inflater(R.layout.activity_main, null);

其他:欢迎补充。

你可能感兴趣的:(android,inflate)