inflate的使用
来源:互联网 发布:青岛知行国际诈骗 编辑:程序博客网 时间:2024/06/05 10:25
一.获得Inflate的方法
a. 通过SystemService获得
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLEATER_SERVICE);
b. 从给定的context中获取
Public static LayoutInflater from(Context context)
二. LayoutInflater.inflate()将Layout文件转换为View,专门供Layout使用的Inflater。虽然Layout也是View的子类,但在android中如果想将xml中的Layout转换为View放入.java代码中操作,只能通过Inflater,而不能通过findViewById()。
三. 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)
四.不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)
4. LinearLayout linearLayout =
(LinearLayout) findViewById(R.id.placeslist_linearlayout);
linearLayout.addView(place_type_text);
一般用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动态加载
- LayoutInflater的使用,inflate
- LayoutInflater的使用,inflate
- LayoutInflater的使用,inflate
- LayoutInflater的使用,inflate
- 浅析inflate的使用
- Android Inflate 的使用
- inflate的使用
- layoutinflate.inflate方法的使用
- Android中inflate的使用:
- inflate之setLayoutParams的使用
- android里的inflate的使用
- Android中LayoutInflater的使用,inflate
- LayoutInflater中inflate方法的简单使用
- Android中LayoutInflater的使用,inflate
- 正确使用inflate,inflate原理解析,LayoutInflater
- inflate函数使用总结
- inflater.inflate使用详解
- 关于LayoutInflater.from(context).inflate()的使用的问题
- coursera《机器学习》吴恩达-week1-01 课程介绍
- 2017.8.10 mqtt 升级代码熟悉
- vmmap工具实现
- 使用Python调试器pdb调试程序(mac)
- 设计模式-装饰模式
- inflate的使用
- Final,finally,finalize的区别
- UI自动化测试经验之谈(五)
- Mongo索引
- mysql里面时间处理函数cast
- 互联网-高可用系列(一)-概念
- 使用Python3批量保存贴吧图片-附爬虫程序
- ida调试win64选择windbg
- Bovine Birthday(思维题)