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 的使用相关推荐

  1. Android LayoutInflater 源码解析

    在上篇文章中我们学习了setContentView的源码,还记得其中的LayoutInflater吗?本篇文章就来学习下LayoutInflater. @Overridepublic void set ...

  2. Android LayoutInflater详解

    Android LayoutInflater详解 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类 似于findViewById().不同点是LayoutInflater是用来 ...

  3. Android LayoutInflater原理分析,带你一步步深入了解View

    Android视图绘制流程完全解析,带你一步步深入了解View(一) 转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/12921889 ...

  4. Android LayoutInflater原理分析,带你一步步深入了解View(一)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/12921889 有段时间没写博客了,感觉都有些生疏了呢.最近繁忙的工作终于告一段落, ...

  5. Android LayoutInflater详解(转)

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  6. Android LayoutInflater源码解析:你真的能正确使用吗?

    版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 好久没写博客了,最近忙着换工作,没时间写,工作刚定下来.稍后有时间会写一下换工作经历.接下来进入本篇主题,本来没想写LayoutInflater的 ...

  7. Android LayoutInflater原理分析,带你一步步深入了解View(一) 郭霖学习摘要

    2019独角兽企业重金招聘Python工程师标准>>> public class MainActivity extends Activity {//----------------- ...

  8. Android LayoutInflater 动态地添加删除View

    我想实现点击一个按钮(或其他的事件)添加或删除View,网上找到了LayoutInflater这个类. 下面是我自己一些经验: android官网上LayoutInflater的API:http:// ...

  9. android service layoutinflater,[转]Android LayoutInflater详解

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  10. android layoutinflater用法,Android LayoutInflater的用法详解

    相信我们在开发过程中肯定接触过LayoutInflater,比如ListView的适配器里的getView方法里通过LayoutInflater.from(Context).inflater来加载xm ...

最新文章

  1. 【IT笔试面试题整理】不用加减乘除做加法
  2. QIIME 2用户文档. 13数据评估和质控Evaluating and controlling(2018.11)
  3. Max-Flow Min-Cut
  4. lscpu命令查看CPU统计信息
  5. 单片机独立式按键c语言程序,(原创)51单片机C语言程序设计--速学教程实例(入门篇)之独立按键(查询)...
  6. 网络服务器安全协议,ipsec 网络安全协议
  7. Ubuntu使用技巧(一)
  8. 关于多库操作一些想法
  9. esp32搭建文件服务器,ESP32入门示例 - SD卡Web服务器
  10. TextRank、BM25算法提取关键字、文章自动摘要优秀文章保存
  11. 10分钟 教你学会Linux/Unix下的vi文本编辑器
  12. rgb颜色查询工具_认识色彩的三要素 理解颜色的此消彼长 合理使用工具改变照片色彩...
  13. Spring Data Jpa的JpaRepository的getOne()方法查询数据实体时报错could not initialize proxy
  14. matlab实验报告的总结,matlab实验报告
  15. JavaSE基础学习
  16. 小米笔记本重装系统BOOT启动菜单识别不了硬盘无法启动进入系统
  17. VS2010提示未能正确加载包
  18. 【Android】App开发-动画效果篇
  19. 无需SVIP,一款快速下载资源的神器
  20. 计算机二级易错知识点 2021 8.8

热门文章

  1. 使用Maven打包生成的-SNAPSHOT.jar与-RELEASE.jar分别代表什么?SNAPSHOT是什么意思?RELEASE是什么意思?
  2. Go 知识点(17)— go 工具链 go test 使用
  3. 改变自己,让自己变得更好
  4. 【spring】spring基于xml的声明式事务控制
  5. LARS 算法简介-机器学习
  6. 使用ONNX将模型转移至Caffe2和移动端
  7. 从C到C++过渡的3个原因
  8. Activity在有Dialog时按Home键的生命周期
  9. 零起点学算法22——华氏摄氏温度转换
  10. Python 的基本数据类型