当前位置:网站首页>在Activity中获取另一个XML文件的控件
在Activity中获取另一个XML文件的控件
2022-08-04 01:06:00 【是小A】
最近,我在项目开发中时常遇到一个问题,那就是如何在一个Activity或者Fragment中获取到其他的XML文件呢?
很多时候,我们都需要在一个Activity中去调用另一个xml布局文件,即非当前Activity所绑定的xml布局文件中的控件,这时候我们就不能直接使用
findViewById(),不然会报错,指向空对象
下面为大家总结了一个方案:
LayoutInflater inflater = LayoutInflater.from (当前类);
View view = inflater.inflate(R.layout.需要引用的XML文件名,null);
Button button = (Button)view.finndViewById(R.id.需要引用的ID名);
如果一行代码写完的话:
Button button = (Button) LayoutInflater.from(当前类).inflate(R.layout.需要获取的xml文件名,null).findViewById(R.id.控件ID);
以上就是关于Activity或者Fragment引用非此类XML文件的内容了,希望能帮助到您!
边栏推荐
- How to copy baby from Taobao (or Tmall store) through API interface to Pinduoduo interface code docking tutorial
- How to find the cause of Fiori Launchpad routing errors by single-step debugging
- 教你如何定位不合理的SQL?并优化之
- 如何用C语言代码实现商品管理系统开发
- 【虚拟户生态平台】虚拟化平台安装时遇到的坑
- .NET Static Code Weaving - Rougamo Release 1.1.0
- pygame 中的transform模块
- 新一代服务网关Gateway的实践笔记
- 字符串的排列
- 【store商城项目01】环境准备以及测试
猜你喜欢
随机推荐
2022 中国算力大会发布“创新先锋”优秀成果
typescript51-泛型的基本使用
Google Earth Engine - Calculates the effective width of rivers using publicly available river data
Electronics manufacturing enterprise deployment WMS what are the benefits of warehouse management system
中原银行实时风控体系建设实践
nodeJs--async/await
typescript57-数组泛型接口
取模运算(MOD)
Shell编程之循环语句(for、while)
fsdbDump用法
【性能优化】MySQL性能优化之存储引擎调优
.NET Static Code Weaving - Rougamo Release 1.1.0
Linux安装mysql最简单教程(一次成功)
数据库扩容也可以如此丝滑,MySQL千亿级数据生产环境扩容实战
【store商城项目01】环境准备以及测试
typescript51 - basic use of generics
Slipper —— 虚点,最短路
微服务的简单介绍
Quickly build a website with static files
OpenCV如何实现Sobel边缘检测