获取当前View
在實(shí)際開發(fā)中LayoutInflater這個(gè)類還是非常有用的,它的作用類似于findViewById()。不同點(diǎn)是LayoutInflater是用來找res/layout/下的xml布局文件,并且實(shí)例化;而findViewById()是找xml布局文件下的具體widget控件(如Button、TextView等)。
具體作用: 1、對于一個(gè)沒有被載入或者想要?jiǎng)討B(tài)載入的界面,都需要使用LayoutInflater.inflate()來載入; 2、對于一個(gè)已經(jīng)載入的界面,就可以使用Activiyt.findViewById()方法來獲得其中的界面元素。 LayoutInflater 是一個(gè)抽象類,在文檔中如下聲明: public abstract class LayoutInflater extends Object 獲得 LayoutInflater 實(shí)例的三種方式 1. LayoutInflater inflater = getLayoutInflater();//調(diào)用Activity的getLayoutInflater()? 2. LayoutInflater inflater = LayoutInflater.from(context); ? 3. LayoutInflater inflater = ?(LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); View view = LayoutInflater.from(context).inflate(R.layout.activity_float,null);?
總結(jié)
- 上一篇: 解决Eclipse中文乱码的方法
- 下一篇: 数据结构学习笔记(2)