zoukankan      html  css  js  c++  java
  • Android——inflate 将一个xml中定义的布局找出来

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

      LayoutInflater layoutInflater = LayoutInflater.from(DialogActivity.this);
                            // 得到自定义对话框
          final View dialogView = layoutInflater.inflate(R.layout.dialog_login, null);


      
      publicViewinflate(intresource,ViewGrouproot,booleanattachToRoot)
      
      在View类中,也有inflate方法
      
      publicstaticViewinflate(Contextcontext,intresource,ViewGrouproot)

    引自:http://android.tgbus.com/Android/tutorial/201104/348009.shtml

  • 相关阅读:
    从国内流程管理软件市场份额看中国BPM行业发展
    为什么流程是成功企业的关键?
    港真,到底应该选择OA还是BPM?
    hello world
    i40e网卡驱动遇到的一个问题
    suse 11 sp3编译报错问题
    linux scsi相关的一些学习笔记
    linux tcp 在timewait 状态下的报文处理
    趋势科技 redirfs模块的一个小问题
    linux 3.10 一个扇区异常可能引发的hung
  • 原文地址:https://www.cnblogs.com/Defry/p/4384402.html
Copyright © 2011-2022 走看看