LayoutInflater作用是将layout的xml布局文件实例化为View类对象。

前端之家收集整理的这篇文章主要介绍了LayoutInflater作用是将layout的xml布局文件实例化为View类对象。前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

获取LayoutInflater方法有如下三种:

LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main, null );
LayoutInflater inflater = LayoutInflater.from(context); (该方法实质就是第一种方法,可参考源代码)
);
LayoutInflater inflater = getLayoutInflater();(在Activity中可以使用,实际上是View子类下window的一个函数
);

使用方法

public class MyInflate extends Activity{
private TextView tv;
void OnCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
//setContentView(R.layout.main);
//tv = (TextView) findViewById(R.id.tv);
LayoutInflater inflate = LayoutInflater.from( this View view = inflate.inflate(R.layout.main,monospace!important; white-space:pre-wrap; float:none!important; border-top-width:0px!important; border-bottom-width:0px!important; height:auto!important; color:#000000!important; font-size:12px!important; vertical-align:baseline!important; border-left-width:0px!important; top:auto!important; right:auto!important; padding-top:0px!important; left:auto!important">setContentView(view);
}
}

猜你在找的XML相关文章