惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CERT Recently Published Vulnerability Notes
D
Docker
GbyAI
GbyAI
博客园 - 三生石上(FineUI控件)
博客园_首页
H
Help Net Security
T
The Exploit Database - CXSecurity.com
Microsoft Security Blog
Microsoft Security Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Privacy & Cybersecurity Law Blog
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
F
Full Disclosure
Blog — PlanetScale
Blog — PlanetScale
Y
Y Combinator Blog
V
Vulnerabilities – Threatpost
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
aimingoo的专栏
aimingoo的专栏
Simon Willison's Weblog
Simon Willison's Weblog
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
S
Security Affairs
T
Tailwind CSS Blog
T
Tor Project blog
W
WeLiveSecurity
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
The Hacker News
The Hacker News
腾讯CDC
M
MIT News - Artificial intelligence
D
DataBreaches.Net
量子位
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
博客园 - 【当耐特】
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
云风的 BLOG
云风的 BLOG
C
Cisco Blogs
I
InfoQ
Engineering at Meta
Engineering at Meta
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
美团技术团队
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
L
LangChain Blog
A
About on SuperTechFans

CAYZLH

MacBook上备忘录APP的快捷键和手势 让Google屏蔽某些搜索结果 WSL自定义安装Ubuntu typora自动上传图片配置 自建giscus服务 Windows下结束指定端口的进程 批量修改maven多模块版本号 使用winsw部署SpringBoot项目 Gson简易指南 docker搭建Consul集群 使用Docker部署SpringBoot项目 Docker使用redis镜像 Docker使用rabbitmq Docker使用zookeeper Docker使用MySQL Dockerfile常用指令 Docker免sudo操作 雷鸟电视去广告 使用ADB卸载MIUI系统应用 利用GitHub做图床 将网站变成灰色 Github上传大文件 Android远程调试命令adb vscode快捷键的使用 SpringBoot文件上传异常处理 SpringBoot封装JedisUtils工具类 SpringBoot自动部署脚本 使用Spring读取文件的几种方式 Spring统一异常返回 利用Github做Maven私服 Redis常用指令 Maven常用指令 使用mysqldump导出数据 SpringBoot动态切换多数据源 Java8的日期处理实践 lambda表达式语法 Stream表达式语法 使用Optional优雅地判空 Android Support vs AndroidX 「Vue」Runtime-Complier和Runtime-only的区别 RecyclerView使用记录 Redis分布式锁的几种方案 打造一个舒服的写作环境(Hexo) 深入了解与使用ThreadLocal Maven中dependencyManagement的作用 Redis配置认证密码 nginx简单配置示例 Linux(macOS)换源 Linux搭建Git服务器 Linux配置ssh使用公钥登录远程服务器 Linux下find与exec的使用 Linux排查Java问题工具单 Linux常用命令 MySql数据库优化细节 API签名验证方案 Java集合框架 Redis基础知识总结 Spring事务管理 Redis为什么这么快 SpringBoot实现Jwt单点登录 SpringBoot实现Redis分布式锁 Spring面向切面编程(知识梳理) SpringBoot异步请求和异步调用 Mybatis中用到的几种设计模式 Docker Swarm RocketMQ相关流程图/原理图 SonarQube的安装与使用 宝塔面板部署vue项目 Tomcat的三种接收请求处理方式 Docker知识扫盲 安装Jenkins并用于部署SpringBoot项目 Tomcat单机多实例部署 Maven中Scope的分类 Springboot集成Shiro(前后端分离) Linux搭建frp服务(内网穿透) Redis高逼格指令 Docker私有仓库的搭建与使用 Dockerfile使用介绍 Redis的使用场景 Docker的网络模式
动态设置布局之LayoutInflater
Ant丶 · 2021-09-08 · via CAYZLH

最近在做Android项目,也没有时间从头开始系统学一遍,大部分知识点只能一边做项目一遍积累。

今天在做一个需求写布局的时候在加藤同学的建议下使用RecyclerView来实现,在编码过程中接触到LayoutInflater这玩意,也算是第一次接触吧,整理下相关知识点。

什么是LayoutInflater

LayoutInflater是一个用于将xml布局文件加载为View或者ViewGroup对象的工具,我们可以称之为**布局加载器**。

layoutxml布局文件实例化为View类对象,LayoutInflater 的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:ButtonTextView等)。

获取方式

LayoutInflater本身是一个抽象类,不能直接通过new的方式来获取它的实例。

上代码:

  • 第一种方法

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  • 第二种方法

    LayoutInflater inflater = LayoutInflater.from(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;
}

Activity内部调用getLayoutInflater方法其实调用的是PhoneWindowmLayoutInflater

public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}

这几个方法实际上殊途同归,都是通过调用ContextgetSystemService方法去获取。

获取到的是PhoneLayoutInflater这个实现类。

public class Policy implements IPolicy {
...
public LayoutInflater makeNewLayoutInflater(Context context) {
return new PhoneLayoutInflater(context);
}
}

实例化View类对象

实例化LayoutInflater之后,就要将layoutxml布局文件实例化为View类对象。

View view=inflater.inflate(R.layout.ID, null);

inflate方法

点击进入sdk源码,可以发现inflate方法有以下几个重载方法:

image-20210908230345814

它们返回的值都是View对象。

其中 public View inflate (int resource, ViewGroup root) 最为常用。

示例代码:

LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 
View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));

EditText editText = (EditText)view.findViewById(R.id.content);

指定了第二个参数 ViewGroup root,当然也可以设置为 null 值。

inflate方法的参数关系

  • root != null, attachToRoot == true

    传进来的布局会被加载成为一个View并作为子View添加到root中,最终返回root;
    而且这个布局根节点的android:layout_xxx参数会被解析用来设置View的大小;

  • root == null, attachToRoot无意义

    当root为空时,attachToRoot无论是什么都没有意义。此时传进来的布局会被加载成为一个View并直接返回;
    布局根View的android:layout_xxx属性会被忽略,即android:layout_xx属性只有依附在某个ViewGroup中才能生效

  • root != null, attachToRoot == false

    传进来的布局会被加载成为一个View并直接返回。
    布局根View的android:layout_xxx属性会被解析成LayoutParams并设置在View上,此时root只用于设置布局根View的大小和位置。

加载xml布局的原理

从根节点开始,递归解析xml的每个节点。

每一步递归的过程是:通过节点名称(全类名),使用ClassLoader创建对应类的实例,也就是View,然后,将这个View添加到它的上层节点(父View)。

同时会解析对应xml节点的属性作为View的属性。每个层级的节点都会被生成一个个的View,并根据View的层级关系add到对应的直接父View(上层节点)中,最终返回一个包含了所有解析好的子View的布局根View