zoukankan      html  css  js  c++  java
  • Android 关于inflate

    通俗的说,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

      publicViewinflate(intresource,ViewGrouproot,booleanattachToRoot)

      在View类中,也有inflate方法

      publicstaticViewinflate(Contextcontext,intresource,ViewGrouproot)


  • 相关阅读:
    bzoj 3438 小M的作物
    洛谷 1126 机器人搬重物
    hdu 4055 Number String
    顺序对齐
    codevs 1300 文件排版
    NOIP 2015 提高组 Day2
    poj 2484 A Funny Game
    51nod 1548 欧姆诺姆和糖果 (制约关系优化枚举)
    hdu 1907 John (anti—Nim)
    hdu 2516 取石子游戏 (斐波那契博弈)
  • 原文地址:https://www.cnblogs.com/bluestorm/p/2298148.html
Copyright © 2011-2022 走看看