Android LayoutInflater 的使用
LayoutInflater 有三种实现方法
1 LayoutInflater inflater = LayoutInflater.from(context) 这种方法常用在弹框
2 LayoutInflater inflater = getLayoutInflater(); 这样方法自己用在Fragment 以用布局
3 LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
下面已popwindow 为例来说下3中方法实现
第一种
public class InflateActivity extends AppCompatActivity {PopupWindow popupWindow;LayoutInflater inflater;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.inflate_activity_layout);inflater = LayoutInflater.from(this);findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {View view = inflater.inflate(R.layout.bottom__pop_item, null);popupWindow = new PopupWindow(view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, true);popupWindow.setContentView(view);View rootview = LayoutInflater.from(InflateActivity.this).inflate(R.layout.inflate_activity_layout, null);popupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);}});}
}
不过一般都不是这样写的太麻烦了一般都是直接写在一起的如下
public class InflateActivity extends AppCompatActivity {PopupWindow popupWindow;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.inflate_activity_layout);findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {View view = LayoutInflater.from(InflateActivity.this).inflate(R.layout.bottom__pop_item, null);popupWindow = new PopupWindow(view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, true);popupWindow.setContentView(view);View rootview = LayoutInflater.from(InflateActivity.this).inflate(R.layout.inflate_activity_layout, null);popupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);}});}
}
第二种方法实现
public class InflateActivity extends AppCompatActivity {PopupWindow popupWindow;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.inflate_activity_layout);findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {View view = getLayoutInflater().inflate(R.layout.bottom__pop_item, null);popupWindow = new PopupWindow(view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, true);popupWindow.setContentView(view);View rootview = LayoutInflater.from(InflateActivity.this).inflate(R.layout.inflate_activity_layout, null);popupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);}});}
}
第三种方法实现
public class InflateActivity extends AppCompatActivity {PopupWindow popupWindow;LayoutInflater inflater;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.inflate_activity_layout);inflater= (LayoutInflater)InflateActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {View view = inflater.inflate(R.layout.bottom__pop_item, null);popupWindow = new PopupWindow(view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, true);popupWindow.setContentView(view);View rootview = LayoutInflater.from(InflateActivity.this).inflate(R.layout.inflate_activity_layout, null);popupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);}});}
}
简单的会使用了在看看inflate 方法
当插入的是一个整体布局,比如弹框之类的
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {return inflate(resource, root, root != null); }
resource 是布局,布局是一个整体,所以呢root 我们填写为null
假如现在要一个布局里面插入另外一个布局 类似下面的
public class InflateActivity extends AppCompatActivity {private LinearLayout cons;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.inflate_activity_layout);cons = findViewById(R.id.cons);getLayoutInflater().inflate(R.layout.inflate_one_layout,cons,true);
}}
这个时候就是使用到了
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
这个root 就是父布局 ,attachToRoot 是,当讲view 添加到父布局中的时候设置为true ,当不想添加如果已经添加过了
设置为false ,,,那使用fragment 的时候为啥设置了false ,这个是因为fragment 里面有个什么方法会自动添加
如果设置为true ,就是添加了2次 所以会报错
Android LayoutInflater 的使用相关推荐
- Android LayoutInflater 源码解析
在上篇文章中我们学习了setContentView的源码,还记得其中的LayoutInflater吗?本篇文章就来学习下LayoutInflater. @Overridepublic void set ...
- Android LayoutInflater详解
Android LayoutInflater详解 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类 似于findViewById().不同点是LayoutInflater是用来 ...
- Android LayoutInflater原理分析,带你一步步深入了解View
Android视图绘制流程完全解析,带你一步步深入了解View(一) 转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/12921889 ...
- Android LayoutInflater原理分析,带你一步步深入了解View(一)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/12921889 有段时间没写博客了,感觉都有些生疏了呢.最近繁忙的工作终于告一段落, ...
- Android LayoutInflater详解(转)
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- Android LayoutInflater源码解析:你真的能正确使用吗?
版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 好久没写博客了,最近忙着换工作,没时间写,工作刚定下来.稍后有时间会写一下换工作经历.接下来进入本篇主题,本来没想写LayoutInflater的 ...
- Android LayoutInflater原理分析,带你一步步深入了解View(一) 郭霖学习摘要
2019独角兽企业重金招聘Python工程师标准>>> public class MainActivity extends Activity {//----------------- ...
- Android LayoutInflater 动态地添加删除View
我想实现点击一个按钮(或其他的事件)添加或删除View,网上找到了LayoutInflater这个类. 下面是我自己一些经验: android官网上LayoutInflater的API:http:// ...
- android service layoutinflater,[转]Android LayoutInflater详解
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- android layoutinflater用法,Android LayoutInflater的用法详解
相信我们在开发过程中肯定接触过LayoutInflater,比如ListView的适配器里的getView方法里通过LayoutInflater.from(Context).inflater来加载xm ...
最新文章
- 【IT笔试面试题整理】不用加减乘除做加法
- QIIME 2用户文档. 13数据评估和质控Evaluating and controlling(2018.11)
- Max-Flow Min-Cut
- lscpu命令查看CPU统计信息
- 单片机独立式按键c语言程序,(原创)51单片机C语言程序设计--速学教程实例(入门篇)之独立按键(查询)...
- 网络服务器安全协议,ipsec 网络安全协议
- Ubuntu使用技巧(一)
- 关于多库操作一些想法
- esp32搭建文件服务器,ESP32入门示例 - SD卡Web服务器
- TextRank、BM25算法提取关键字、文章自动摘要优秀文章保存
- 10分钟 教你学会Linux/Unix下的vi文本编辑器
- rgb颜色查询工具_认识色彩的三要素 理解颜色的此消彼长 合理使用工具改变照片色彩...
- Spring Data Jpa的JpaRepository的getOne()方法查询数据实体时报错could not initialize proxy
- matlab实验报告的总结,matlab实验报告
- JavaSE基础学习
- 小米笔记本重装系统BOOT启动菜单识别不了硬盘无法启动进入系统
- VS2010提示未能正确加载包
- 【Android】App开发-动画效果篇
- 无需SVIP,一款快速下载资源的神器
- 计算机二级易错知识点 2021 8.8