您的位置:首页 > 其它

LayoutInflater 动态加载布局文件

2013-08-01 09:15 399 查看


LayoutInflater有三种方式加载布局文件:

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main, null)



LayoutInflater inflater = LayoutInflater.from(context);
View layout = inflater.inflate(R.layout.main, null);



LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.main, null);


 

inflater.inflate(resourceId, rootId);第一个参数是要加载布局的id,第二个参数时给指定布局的外部套一层父布局,如果不需要就直接传null

另外,还有一个方法,inflater.inflate(resourceId, rootId, attachToRoot)

注意点:LayoutInflater加载的布局文件最好套上一个parent layout,否则在view上设置的layout_width和layout_height将不起作用,因为layout_类别的视图属性是设置view在父视图中的大小的,并不是指定view本身的大小。


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: