inflate获取view 的几种方法
来源:互联网 发布:三七互娱广州待遇知乎 编辑:程序博客网 时间:2024/06/06 07:13
通俗的说,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)
因为在一个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)
0 0
- inflate获取view 的几种方法
- View inflate 方法与 LayoutInflater inflate 方法的区别详解
- View.inflate和LayoutInflater的inflate方法区别
- (转)View.inflate和LayoutInflater的inflate方法区别
- Android--获取View的宽高的几种方法
- Android获取View坐标的几种方法
- android获取view宽高的几种方法 - ysl_longer
- Android 获取View高度的几种方法
- View中inflate方法
- inflate xml 方式获取 View 对象的过程?
- android获取view宽高的几种方法,解决获取高宽为0问题
- View类的方法inflate和LayoutInflater类的inflate方法
- 获取View高度的几种方式
- LayoutInflater.inflate方法加载View
- View.inflate()与LayoutInflater.inflate()的区别?
- [Android] 一种优化view inflate耗时的方法
- android 开发中获取View视图的宽和高的几种方法
- android 开发中获取View视图的宽和高的几种方法
- AtomicInteger简介
- android数据读写
- ul模拟select,jquery表单美化代码
- WP程序常用PHP代码列举送
- ubuntu软件中心崩溃问题
- inflate获取view 的几种方法
- ZJU-PAT 1078. Hashing (25) 浙大2014年上机复试第二题
- 计算机存储层次
- 一个单片机C 程序 出租车计价器实现
- C++编程->异常处理的三重境界
- cvbnbnmbnmbmmn
- 如何计算两个文档的相似度(一)
- xftp 4.0注册码
- Google Guava 库用法整理