Android Inflate方法

  通俗的说,inflate就相当于将一个xml中定义的布局找出来.在一个Activity里如果直接用findViewById()的话,相应的是setConentView()所对应layout里的组件.如果不是,你就必须用inflate()先将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(intresource,ViewGrouproot,booleanattachToRoot)

更多相关文章

  1. Android(安卓)自定义RecyclerView 实现真正的Gallery效果
  2. Android(安卓)listview 实现列表多选
  3. android 控制震动强度
  4. Android使用广播(BroadCast)实现强制下线的方法
  5. Google Map API Key 获得方法
  6. Android开发8:UI组件TextView,EditText,Button
  7. android 植入点乐广告方法
  8. UI与线程交互
  9. Android_Layout_xml布局

随机推荐

  1. Android(安卓)打印日志封装库
  2. [置顶] Android系统移植与调试之------->
  3. 如何用eclipse编译调试adnroid的Browser
  4. Android(安卓)Permission 中文说明
  5. Android(安卓)WebView和EditText焦点冲突
  6. spinner 样式问题解决
  7. Android(安卓)编译时注解 —— 语法详解
  8. Android电量测试工具battery-historian学
  9. [置顶] android下调试声卡驱动之wm8960介
  10. android opengl 播放 yuv数据