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

简介: 引用:http://blog.chinaunix.net/uid-27024249-id-3304935.html Android之Inflate()方法用途 Inflate()作用就是将xml定义的一个布局找出来并隐藏起来,并没有显示出来。

引用:http://blog.chinaunix.net/uid-27024249-id-3304935.html

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来进行动态加载了。

 

点击(此处)折叠或打开

  1. < TextView
  2. android:id="@+id/tview"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:text="ATAAW.COM"
  6. />
  7. < Button
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:id="@+id/button"
  11. android:text="按钮"
  12. />
在程序中动态加载以上布局。

 

 

点击(此处)折叠或打开

  1. LayoutInflater flater = LayoutInflater.from(this);
  2. View view = flater.inflate(R.layout.example, null);
获取布局中的控件。

 

 

点击(此处)折叠或打开

  1. button = (Button) view.findViewById(R.id.button);
  2. 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. 两者的区别:实际上是一样的,举例如下:

点击(此处)折叠或打开

  1. public static LayoutInflater from(Context context) { 
  2.         LayoutInflater LayoutInflater = 
  3.                 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
  4.         if (LayoutInflater == null) { 
  5.             throw new AssertionError("LayoutInflater not found."); 
  6.         } 
  7.         return LayoutInflater; 
  8. }
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文件。
相关文章
|
Android开发 开发者
Android自定义view之利用drawArc方法实现动态效果
本文介绍了如何通过Android自定义View实现动态效果,重点使用`drawArc`方法完成圆弧动画。首先通过`onSizeChanged`进行测量,初始化画笔属性,设置圆弧相关参数。核心思路是不断改变圆弧扫过角度`sweepAngle`,并调用`invalidate()`刷新View以实现动态旋转效果。最后附上完整代码与效果图,帮助开发者快速理解并实践这一动画实现方式。
375 0
|
存储 Shell Android开发
基于Android P,自定义Android开机动画的方法
本文详细介绍了基于Android P系统自定义开机动画的步骤,包括动画文件结构、脚本编写、ZIP打包方法以及如何将自定义动画集成到AOSP源码中。
1494 2
基于Android P,自定义Android开机动画的方法
|
XML 编解码 Android开发
非常经典的Android开发问题-mipmap图标目录和drawable图标目录的区别和适用场景实战举例-优雅草卓伊凡
非常经典的Android开发问题-mipmap图标目录和drawable图标目录的区别和适用场景实战举例-优雅草卓伊凡
1061 0
非常经典的Android开发问题-mipmap图标目录和drawable图标目录的区别和适用场景实战举例-优雅草卓伊凡
|
Android开发
基于android-11.0.0_r39,系统应用的手动签名方法和过程
本文介绍了基于Android 11.0.0_r39版本进行系统应用手动签名的方法和解决签名过程中遇到的错误,包括处理`no conscrypt_openjdk_jni-linux-x86_64`和`RegisterNatives failed`的问题。
1198 2
Android.mk(makefile)中几个符号的区别:=、 :=、 ?=、 +=
本文解释了在Android.mk文件中使用的几种赋值符号的区别,包括`=`(基本赋值)、`:=`(覆盖赋值)、`?=`(条件赋值,仅在变量未赋值时操作)、`+=`(追加赋值),并通过实验演示了这些符号的具体行为和效果。
1317 1
|
安全 数据库 Android开发
在Android开发中实现两个Intent跳转及数据交换的方法
总结上述内容,在Android开发中,Intent不仅是活动跳转的桥梁,也是两个活动之间进行数据交换的媒介。运用Intent传递数据时需注意数据类型、传输大小限制以及安全性问题的处理,以确保应用的健壯性和安全性。
860 11
|
Android开发 Kotlin
Android经典面试题之Kotlin的==和===有什么区别?
本文介绍了 Kotlin 中 `==` 和 `===` 操作符的区别:`==` 用于比较值是否相等,而 `===` 用于检查对象身份。对于基本类型,两者行为相似;对于对象引用,`==` 比较值相等性,`===` 检查引用是否指向同一实例。此外,还列举了其他常用比较操作符及其应用场景。
650 94
|
缓存 Java Shell
Android 系统缓存扫描与清理方法分析
Android 系统缓存从原理探索到实现。
1251 15
Android 系统缓存扫描与清理方法分析
|
编译器 Android开发 开发者
Android经典面试题之Kotlin中Lambda表达式和匿名函数的区别
Lambda表达式和匿名函数都是Kotlin中强大的特性,帮助开发者编写简洁而高效的代码。理解它们的区别和适用场景,有助于选择最合适的方式来解决问题。希望本文的详细讲解和示例能够帮助你在Kotlin开发中更好地运用这些特性。
568 9
|
Android开发
Android在rootdir根目录创建自定义目录和挂载点的方法
本文介绍了在Android高通平台的根目录下创建自定义目录和挂载点的方法,通过修改Android.mk文件并使用`LOCAL_POST_INSTALL_CMD`变量在编译过程中添加目录,最终在ramdisk.img的系统根路径下成功创建了`/factory/bin`目录。
1284 11

热门文章

最新文章