Android学习笔记 LayoutInflater

来源:互联网 发布:什么软件可以制作漫画 编辑:程序博客网 时间:2024/06/03 12:28
  1. 具体作用:

    • 对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
    • 对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。
  2. 获得 LayoutInflater 实例的三种方式:

    • LayoutInflater inflater = getLayoutInflater();//调用Activity的getLayoutInflater()
    • LayoutInflater inflater = LayoutInflater.from(context)
    • LayoutInflater inflater = (LayoutInflater)context.getSystemService
      (Context.LAYOUT_INFLATER_SERVICE)
  3. inflate方法:

    public View inflate(int Resourece,ViewGroup root)作用:填充一个新的视图层次结构从指定的XML资源文件中reSource:View的layout的IDroot: 生成的层次结构的根视图return 填充的层次结构的根视图如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。其余几个重载的inflate函数类似。
LayoutInflater inflate = LayoutInflater.from(this);View view = inflate.inflate(R.layout.main,null);setContentView(view);
0 0
原创粉丝点击