关于inflate,获取布局xml
来源:互联网 发布:淘宝详情页模板下载 编辑:程序博客网 时间:2024/04/29 22:05
通俗的说,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)
转自:http://daydayup1989.javaeye.com/blog/824735
- 关于inflate,获取布局xml
- 关于inflate,获取布局xml
- 使用inflate获取XML布局文件
- ViewPager中动态添加XML布局文件,并获取XML文件中控件ID的操作(inflate)
- inflate xml 方式获取 View 对象的过程?
- Inflate加载布局文件
- Inflate加载布局文件
- 1. 一个界面xml布局文件应仅inflate一次即可
- 【android】LayoutInflater.inflate方法的详解及xml根元素的布局参数不起作用的问题
- 【android】LayoutInflater.inflate方法的详解及xml根元素的布局参数不起作用的问题
- LayoutInflater和inflate()方法的用法 (自定义View中加载xml布局)
- LayoutInflater和inflate()方法的用法 (自定义View中加载xml布局)
- LayoutInflater和inflate()方法的用法 (自定义View中加载xml布局)
- Android XML布局文件报错Unable to inflate view tag without class attribute
- 关于inflate导入Layout失真(导入的布局,不是自己想要的)
- Android 关于inflate
- Android--关于inflate()方法
- Android 关于inflate
- 数字上变频 数字下变频
- Ubuntu下Hadoop单机部署及分布式集群部署
- sql复杂查询语句总结
- Android声音焦点----从音乐回到Luncher调节音量显示的是Music的音量
- android的shader渲染器
- 关于inflate,获取布局xml
- Render的mode可以设为两种模式
- iOS正则表达式简介
- Android 操作系统中的内存回收策略
- Http :超文本传输协议 HyperText Transfer Protocol
- Android安全机制--四大组件安全
- Longest Valid Parentheses Java
- js判断是否为pc端或移动端
- 基于Web的登录功能设计测试用例