LayoutInflater 动态加载布局文件

来源:互联网 发布:网络语狗粮是什么意思 编辑:程序博客网 时间:2024/06/06 00:32

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本身的大小。


原创粉丝点击