关于LayoutInflater的方法讲解
来源:互联网 发布:java相关的搜索引擎 编辑:程序博客网 时间:2024/04/29 18:17
Android 关于inflate
通俗的说,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(intresource,ViewGrouproot,booleanattachToRoot)
在View类中,也有inflate方法
public static View inflate(Contextcontext,intresource,ViewGrouproot)
因为在一个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(intresource,ViewGrouproot,booleanattachToRoot)
在View类中,也有inflate方法
public static View inflate(Contextcontext,intresource,ViewGrouproot)
阅读全文
0 0
- 关于LayoutInflater的方法讲解
- 关于LayoutInflater的inflate方法分析
- 关于LayoutInflater的用法
- 关于LayoutInflater的总结
- 关于FragmentPagerAdapter的方法讲解
- 关于LayoutInflater 的inflate方法参数的含义
- 关于LayoutInflater的inflate方法和LayoutParam分析(二)
- LayoutInflater的inflate方法
- LayoutInflater的inflate方法
- LayoutInflater的获得方法
- LayoutInflater的inflate方法
- LayoutInflater的inflate函数用法讲解
- 关于LayoutInflater的错误用法
- 关于LayoutInflater的一些总结
- 关于LayoutInflater的一些梗
- 关于Android LayoutInflater的用法
- 关于LayoutInflater的错误用法
- 关于 android 中 postDelayed方法的讲解
- v-if
- android -- 蓝牙 bluetooth
- ExtJS 4.2 报错 layout run failed 的解决办法
- java 读取图片显示红色
- lv相关命令
- 关于LayoutInflater的方法讲解
- ssm分布式架构电商项目(一)
- android -- 蓝牙 bluetooth (二) 打开蓝牙
- Linux下的一些实用的工具
- Android 于Webview 交互
- poj-3049
- 日期滚动选择(使用DatePicker实现)
- ACM第一次比赛题目及标准程序(基础水题)
- Win10环境下安装Redis