LayoutInflater学习总结

----参考,转载,借用来源:http://blog.csdn.net/guolin_blog/article/details/12921889布局

Activity类中onCreate方法中,setContentView()方法,之因此是setContentView,是由于系统会自动在咱们的Activity的布局文件的最外层包一层FrameLayout,因此方法才是setContentView,而不是setView,具体原理解析能够用下面这张图来表示:.net

而咱们常常在xml布局文件中设置的layout_width,layout_height 其实相对父布局而言的,因此咱们最外层的layout_width以及layout_height属性之因此有效果,是由于系统自动在最外面套了一层FrameLayout。xml

因此在咱们自定义一个view,并添加到当前布局的时候须要本身手动的传入一个LayoutParameter,才能设置大小,左右边距等等属性。。----待验证blog

 

2. LayoutInflater的使用方法:资源

  a. LayoutInflater inflater = LayoutInflater.from(context);get

  b. LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);it

a方法是b方法的精简版原理

 

3.inflate方法的重构重构

  inflater(resouceId,root) : resourceId是指要加载的布局的资源文件Id,root指的是给该布局外层再嵌套一个父布局,若是不须要就直接传入null。这样就成功建立了一个布局的实例,以后再将它动态添加到父布局中就能够了。方法

  inflater(resouceId,root,attachToRoot): 前两个参数和上面同样:

    a. 若是root为null,则attachToRoot失去做用,设置成任何值都没有同样

    b. 若是root不为null,attachToRoot为true,则为布局添加一个父布局,即root

    c. 若是root不为null,attachToRoot为false,则会将该布局的layout_width及layout_height属性设置成最外层的布局的两个属性一致。即若是最外层是match_parent,那么该布局也是match_parent

    d. 不设置attachToRoot,若是root不为null,则attachToRoot默认为true;

相关文章
相关标签/搜索