zoukankan      html  css  js  c++  java
  • infate

    通俗的说,inflate就相当于将一个xml中定义的布局找出来.
      
      因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件.
      
      因此如果你的Activity里如果用到别的layout,比如对话框上的layout,你还要设置对话框上的layout里的组件(像图片ImageView,文字TextView)上的内容,你就必须用inflate()先将对话框上的layout找出来,然后再用这个layout对象去找到它上面的组件,如:
      
      View view=View.inflate(this,R.layout.dialog_layout,null);
      
      TextView dialogTV=(TextView)view.findViewById(R.id.dialog_tv);
      
      dialogTV.setText("abcd");
      
      如果组件R.id.dialog_tv是对话框上的组件,而你直接用this.findViewById(R.id.dialog_tv)肯定会报错.
      
      三种方式可以生成LayoutInflater:
      
      LayoutInflater inflater=LayoutInflater.from(this);
      
      LayoutInflater inflater=getLayoutInflater();
      
      LayoutInflater inflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
      
      然后调用inflate方法将xml布局文件转成View
      
      public View inflate(int resource,ViewGroup root,boolean attachToRoot)
      
      在View类中,也有inflate方法
      
      public static View inflate(Context context,int resource,ViewGroup root)

  • 相关阅读:
    python3安装crypto出错,及解决方法
    php中的引用
    算法
    HTTP协议
    jdk 1.8 InvocationHandler 中文注释
    Java实现多线程的几种方法
    shell编写显示ps相关脚本
    逆波兰表达式求值(后序表达式)
    155. 最小栈(leetcode简单题)
    字符串逆序
  • 原文地址:https://www.cnblogs.com/qingblog/p/2616918.html
Copyright © 2011-2022 走看看