Android开发中setContentView和inflate的区别分析

 更新时间:2016年07月06日 10:15:01   作者:与时俱进  
这篇文章主要介绍了Android开发中setContentView和inflate的区别,较为详细的分析了setContentView和inflate的功能、用法及二者的区别,需要的朋友可以参考下

本文实例讲述了Android开发中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等)

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android编程之activity操作技巧总结》、《Android视图View技巧总结》、《Android操作SQLite数据库技巧总结》、《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android文件操作技巧汇总》、《Android编程开发之SD卡操作方法汇总》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • Android编程自定义Notification实例分析

    Android编程自定义Notification实例分析

    这篇文章主要介绍了Android编程自定义Notification的用法,结合实例形式简单分析了自定义Notification的具体功能与实现技巧,需要的朋友可以参考下
    2015-12-12
  • android studio与手机连接调试步骤详解

    android studio与手机连接调试步骤详解

    这篇文章主要为大家详细介绍了android studio与手机连接调试步骤,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Android ViewPager实现图片轮播效果

    Android ViewPager实现图片轮播效果

    这篇文章主要为大家详细介绍了Android ViewPager实现图片轮播效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • 老生常谈Listview中onItemClick中的各个参数(推荐)

    老生常谈Listview中onItemClick中的各个参数(推荐)

    下面小编就为大家带来一篇老生常谈Listview中onItemClick中的各个参数(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • android多种滑动冲突的解决方案

    android多种滑动冲突的解决方案

    本篇文章主要介绍了android多种滑动冲突的解决方案,解决方案主要有2种,外部拦截法 和内部拦截法,有兴趣的可以了解一下。
    2017-02-02
  • android实现人脸识别技术的示例代码

    android实现人脸识别技术的示例代码

    本篇文章主要介绍了android人脸识别技术的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • Android图片缓存之初识Glide(三)

    Android图片缓存之初识Glide(三)

    这篇文章主要为大家详细介绍了Android图片缓存之Glide,学习比较优秀的图片缓存开源框架,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • Flutter控件之实现Widget基类的封装

    Flutter控件之实现Widget基类的封装

    在实际的开发中,Widget的基类还是很有必要存在的,不然就会存在很多的冗余嵌套代码,本文为大家介绍了Flutter中基类是如何封装的,需要的可以收藏一下
    2023-05-05
  • MobPush Android常见问题解决分析

    MobPush Android常见问题解决分析

    这篇文章主要介绍了MobPush Android常见问题解决分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • Android 路径查询具体实现

    Android 路径查询具体实现

    可以通过RasterMap的getDirection()方法来查询路径,和查询地址类似,路径查询的结果也是通过回调函数的方式来通知应用程序的,下面的例子返回南京到北京的路径
    2013-10-10

最新评论