其他分享
首页 > 其他分享> > LayoutInflater工作

LayoutInflater工作

作者:互联网

加载布局的时候通常都是在Activity中调用setContentView()方法来完成的。
其实setContentView()方法的内部也是使用LayoutInflater来加载布局的。

LayoutInflater的基本使用
1.获取LayoutInflater的实例
LayoutInflater layoutInflater=LayoutInflater.from(context);
2.调用inflate()方法来加载布局
layoutInflater.inflate(resourceId,root);
第一个参数是布局id,第二个参数是给该布局外部再嵌套一层父布局。如果不需要就设为null。

inflate(int resource,ViewGroup root,boolean attachToRoot)的解析
1.如果root为null,attachToRoot将会失去作用,无意义。
2.如果root不为null,attachToRoot设为true.则会给加载的布局文件指定一个父布局,即root。
3.如果root不为null,attachToRoot设为false,则会将布局文件最外层的所有layout属性进行设置,当该view被添加到父view中时,这些layout属性会自动失效。
4.在不设置attachToRoot参数的情况下,如果root不为null,attachToRoot参数默认为true。

button中的layout_width和layout_height属性是指定它在布局中的大小,但是最外层布局也有layout_width和layout_heigth属性,它们起作用是因为在setContentView()时,Android会自动在布局文件的最外层再嵌套一个FrameLayout。

标签:layout,LayoutInflater,布局,attachToRoot,工作,null,root
来源: https://www.cnblogs.com/dearnotes/p/12383421.html