【转】Android之Inflate()方法用途+setContentView和inflate区别

转自:http://hi.baidu.com/hanwujisc/item/fe666bc2c97f0e50ac00ef4b

Android之Inflate()方法用途

Inflate()作用就是将xml定义的一个布局找出来,但仅仅是找出来而且隐藏的,没有找到的同时并显示功能。最近做的一个项目就是这一点让我迷茫了好几天。

android上还有一个与Inflate()类似功能的方法叫findViewById(),二者有时均可使用,但也有区别

区别在于:

如果你的Activity里用到别的layout,比如对话框layout,你还要设置这个layout上的其他组件的内容,你就必须用inflate()方法先将对话框的layout找出来,然后再用findViewById()找到它上面的其它组件。例如:

View view1=View.inflate(this,R.layout.dialog_layout,null);

TextView dialogTV=(TextView)view1.findViewById(R.id.dialog_tv);

dialogTV.setText("abcd");

注:R.id.dialog_tv是在对话框layout上的组件,而这时若直接用this.findViewById(R.id.dialog_tv)肯定会报错。

Inflate()或可理解为“隐性膨胀”,隐性摆放在view里,inflate()前只是获得控件,但没有大小没有在View里占据空间,inflate()后有一定大小,只是处于隐藏状态。



setContentView和inflate区别

一般用LayoutInflater做一件事:inflate

inflate这个方法总共有四种形式(见下面),目的都是把xml表述的layout转化为View对象。
其中有一个比较常用,View inflate(int resource, ViewGroup root),另三个,其实目的和这个差不多。

int resource,也就是resource/layout文件在R文件中对应的ID,这个必须指定。
而ViewGroup root则可以是null,null时就只创建一个resource对应的View,不是null时,会将创建的view自动加为root的child。

setContentView和inflate区别:
setContentView()一旦调用, layout就会立刻显示UI;而inflate只会把Layout形成一个以view类实现成的对象,有需要时再用setContentView(view)显示出来
一般在activity中通过setContentView()将界面显示出来,但是如果在非activity中如何对控件布局设置操作了,这需LayoutInflater动态加载
< TextView
android:id="@+id/tview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ATAAW.COM"
/>
< Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="按钮"
/>
在程序中动态加载以上布局。
LayoutInflater flater = LayoutInflater.from(this);
View view = flater.inflate(R.layout.example, null);
获取布局中的控件。
button = (Button) view.findViewById(R.id.button);
textView = (TextView)view.findViewById(R.id.tview);
***********************************************************
接下来结合源码说说inflate方法的四种形式:
inflate 方法总共有四种形式,把xml表达的layout转化为view. This class is used to instantiate layout xml files into its corresponding view object. It is never be used directly——use getLayoutInflater() or getSystemService(String)getLayoutInflate() or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up that is already hook up to the current context and correct configured for the device you are running on. 
1. Context.public abstract object getSystemService(String name) 
2. 两种获得LayoutInflater的方法 
a. 通过SystemService获得 
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLEATER_SERVICE); 
b. 从给定的context中获取 
Public static LayoutInflater from(Context context) 
c. 两者的区别:实际上是一样的,源码 
/** 
     * Obtains the LayoutInflater from the given context. 
     */ 
    public static LayoutInflater from(Context context) { 
        LayoutInflater LayoutInflater = 
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        if (LayoutInflater == null) { 
            throw new AssertionError("LayoutInflater not found."); 
        } 
        return LayoutInflater; 

3. LayoutInflater.inflate()将Layout文件转换为View,专门供Layout使用的Inflater。虽然Layout也是 View的子类,但在android中如果想将xml中的Layout转换为View放入.java代码中操作,只能通过Inflater,而不能通过 findViewById()。 
4. LinearLayout linearLayout = 
(LinearLayout) findViewById(R.id.placeslist_linearlayout); 
linearLayout.addView(place_type_text); 
5. findViewById有两种形式 
R.layout.xx 是引用res/layout/xx.xml的布局文件(inflate 方法),R.id.xx是引用布局文件里面的组件,组件的id是xx(findViewById方法)。所有的组件id都能用R.id.xx来查看,但是 组件不在setContentView()里面的layout中就无法使用,Activity.findViewById()会出现空指针异常 
a. activity中的findViewById(int id) 
b. View 中的findViewById(int id) 
6.不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体widget控件(如:Button,TextView等)。 


个 人总结:上面那句话说得很好,inflator是用来实例化layout下的xml文件的,findViewById()是寻找对应id的widget控 件的。个人把inflator看成是xml文件的句柄,用来抽象地引用这个文件。因为默认的inflator对应的Context就是目前 setContentView对应的这个Layout所在的xml文件。

作者:FreeAquar
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/FreeAquar/p/2804170.html