zoukankan      html  css  js  c++  java
  • 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。

  • 相关阅读:
    VS Code中编写C
    Latex
    JAVA学习-----容器和数据结构
    Markdown2最最基本操作说明(未完待续)
    [lua] table.sort(_table, comp)使用要点
    [coco2d]pageView:addPage时,page无法对齐
    [cocos2d]修改富文本文本和高度
    [cocos2d]格式化获取当前layer的控件名
    [c++]牛客刷题记录2.18
    [c++]STL学习
  • 原文地址:https://www.cnblogs.com/dearnotes/p/12383421.html
Copyright © 2011-2022 走看看