博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【转载】setContentView和inflate区别
阅读量:4314 次
发布时间:2019-06-06

本文共 2732 字,大约阅读时间需要 9 分钟。

原文: (文糙理细……)

  一般用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 
6

在程序中动态加载以上布局。

  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.

/*  对inflate方法的说明:  *    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. findViewById和LayoutInflater的区别  

  不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化;

  而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。

转载于:https://www.cnblogs.com/annaivsu/p/3557765.html

你可能感兴趣的文章
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>
第五天站立会议内容
查看>>
CentOs7安装rabbitmq
查看>>
(转))iOS App上架AppStore 会遇到的坑
查看>>
解决vmware与主机无法连通的问题
查看>>
做好产品
查看>>
项目管理经验
查看>>
笔记:Hadoop权威指南 第8章 MapReduce 的特性
查看>>
JMeter响应数据出现乱码的处理-三种解决方式
查看>>
获取设备实际宽度
查看>>
Notes on <High Performance MySQL> -- Ch3: Schema Optimization and Indexing
查看>>
Alpha冲刺(10/10)
查看>>
数组Array的API2
查看>>
为什么 Redis 重启后没有正确恢复之前的内存数据
查看>>
No qualifying bean of type available问题修复
查看>>
第四周助教心得体会
查看>>
spfile
查看>>
Team Foundation Service更新:改善了导航和项目状态速查功能
查看>>
WordPress资源站点推荐
查看>>