LayoutInflater.from(this)、inflate 详解
三种方式可以生成LayoutInflater
LayoutInflater inflater = LayoutInflater.from(this);
LayoutInflater inflater = getLayoutInflater();
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
然后调用inflate方法将xml布局文件转成View
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
在View类中,也有inflate方法
public static View inflate (Context context, int resource, ViewGroup root)
通俗的说,inflate就相当于将一个xml中定义的布局找出来.
因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件.
因此如果你的Activity里如果用到别的layout,比如对话框上的layout,你还要设置对话框上的layout里的组件(像图片ImageView,文字TextView)上的内容,你就必须用inflate()先将对话框上的layout找出来,然后再用这个layout对象去找到它上面的组件,如:
View view = View.inflate(this, R.layout.dialog_layout, null);
TextView dialogTV = (TextView) view.findViewById(R.id.dialog_tv);
dialogTV.setText("abcd");
如果组件R.id.dialog_tv是对话框上的组件,而你直接用this.findViewById(R.id.dialog_tv)肯定会报错.
LayoutInflater.from(this)、inflate 详解相关推荐
- Android LayoutInflater.inflate详解
1. 作用 官方释义 Inflate a new view hierarchy from the specified xml resource 大概意思就是从给定的xml中加载view树. 2. 用法 ...
- LayoutInflater.inflate()详解
学习自定义View一段时间了,从开始的一窍不通到现在终于能写出点东西了,前面也写过几篇关于自定义view的博客,但是感觉这东西吧,一天不敲又忘记了,所以准备写一篇自定义View系列的博客,这也算是我这 ...
- LayoutInflater的inflate函数用法详解
LayoutInflater的inflate函数用法详解 LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: ...
- LayoutInflater inflate参数详解
LayoutInflater 类概述: 实例化一个XML布局文件到相应的View对象,并不直接使用.使用getLayoutInflater()或getSystemService(String)来获取一 ...
- Android LayoutInflater详解
Android LayoutInflater详解 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类 似于findViewById().不同点是LayoutInflater是用来 ...
- 安卓 LayoutInflater详解
导读 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById(). 不同点是: LayoutInflater是用来找res/layout/下的xml布局文 ...
- inflater.inflate使用详解
原文地址:https://possiblemobile.com/2013/05/layout-inflation-as-intended/ Layout inflation is the term u ...
- layoutinflater详解
layoutinflater ListView的Adapter的getView方法中基本都会出现,使用inflate方法去加载一个布局,用于ListView的每个Item的布局,但是这三个方法究竟有什 ...
- android jar 包 意见反馈功能,android重点jar包详解.docx
android重点jar包详解 深入理解View(一):从setContentView谈起 我们都知道?MVC,在Android中,这个?V?即指View,那我们今天就来探探View的究竟.在onCr ...
最新文章
- 清华、北大教授同台激辩:脑科学是否真的能启发AI?
- rabbitmq简单收发服务搭建
- 【快乐水题】1518. 换酒问题
- 使用log4j2免费分配日志记录
- kafka实现组内广播
- Understanding Linux CPU Load - when should you be worried?
- 获取文件夹中所有文件的文件名[重复]
- SVN 安装与使用教程总结
- poj3616(Milking Time)
- 在unity中生成螺旋线的效果
- 计算机焊板原理,计算机组成原理电路板焊接(74LS373和74LS138).doc
- 航天器平台二次电源系统的可靠性设计
- Cisco 思科模拟器命令
- 手撸Mybatis源码-基础版
- 全国大学生物联网设计竞赛作品 | 智慧养鸡小助手
- GUET第五组2021——随机森林
- Linux 操作系统的安装
- 黑马程序员——阿龙的学习历程——Java初见
- Windows文件及文件夹命名规则之admini~1≈administrator问题
- CarbonData 使用性能测试