在一般项目中使用adapter时,加载item布局咱们一般会使用:

LayoutInflater.from(context).inflate(R.layout.list_item, null);  

但这样你会发现编译器不希望你这样:Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element)

而且你的xml的最外层布局的一些对于其父布局的一些诉求属性,不管怎么设置都不起作用。

比如这样一个item布局:

<</span>LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="50dp"  android:layout_margin="50dp"  android:gravity="center"  android:orientation="horizontal" >  <</span>TextView  android:id="@+id/textView1"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="TextView1" />  <</span>TextView  android:id="@+id/textView2"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="TextView2" />  </</span>LinearLayout>  

用上述方法加载后结果发现

html] view plaincopy

  1. android:layout_height="50dp"
  2. android:layout_margin="50dp"
    这两句没有效果。

so,咱们来研究一下。

网上流传了这样一篇文章,

http://www.doubleencore.com/2013/05/layout-inflation-as-intended/

点击打开链接

这是一个老外对inflate()的研究。

其实他有很多个方法,但查看源码这些方法都殊途同归。

我现在就说一下inflate(int resource, ViewGroup root, boolean attachToRoot)

第一个参数无需过多解释。

第二个参数指的是加载布局的root

Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (ifattachToRoot is false.)

大概就是说如果后面attachToRoot为true的情况下,这个布局会被解析并加载在root下面,如果为false,则会依照root去解析该xml并返回view,但是这个view不会被加载到root里。

其实如果为false,就是讲xml解析了,并依照root的类型给生成的view set一个LayoutParams ,但不将其add到root里。

然后咱们看源代码里

[html]  view plain copy
  1. LayoutInflater.from(context).inflate(R.layout.list_item, null);

这个其实 是这样调用的:

[java]  view plain copy
  1. public View inflate(int resource, ViewGroup root) {
  2. return inflate(resource, root, root != null);
  3. }

所以我建议将其写为

[java]  view plain copy
  1. LayoutInflater.from(context).inflate(R.layout.list_item,root,false);

root就是加载这个view的父布局。

然后再在listview的adapter试一下,

[java]  view plain copy
  1. @Override
  2. public View getView(int position, View convertView, ViewGroup parent) {
  3. if (convertView == null) {
  4. convertView = LayoutInflater.from(context).inflate(
  5. R.layout.list_item, parent, false);
  6. }
  7. return convertView;
  8. }

发现在listview里加载item的布局,

[html]  view plain copy
  1. android:layout_height="50dp"

这句已经起到作用,但layout_margin无效果。

这是因为在listview里,convertview用的是viewgroup的 LayoutParams,所以线性布局的一些属性,例如layout_margin在解析的时候不起作用的。

为了验证一下,咱们在linearlayout中实验一下LayoutInflater

[java]  view plain copy
  1. layout=(LinearLayout)findViewById(R.id.layout1);
  2. View view=LayoutInflater.from(this).inflate(R.layout.list_item,layout , false);
  3. layout.addView(view);

这样的话设置宽高和设置layout_margin都起到了作用。因为这时候view的layoutParams是LinearLayout.layoutParams的缘故,所以layout_margin果断会起到效果。

奥,对了,注意,在listview中不要将inflate(int resource, ViewGroup root, boolean attachToRoot)的attachToRoot设为true,

因为这样等于说让listview addview(convertView),但是listview不能加子控件,会报如下错误:

[html]  view plain copy
  1. java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

而在linearlayout里可以将inflate(int resource, ViewGroup root, boolean attachToRoot)的attachToRoot设为true,这样就相当于

[java]  view plain copy
  1. View view=LayoutInflater.from(this).inflate(R.layout.list_item,layout , false);
  2. layout.addView(view);

最后附上demo下载地址http://download.csdn.NET/detail/ccfcccfc/8142913点击打开链接

转自:http://blog.sina.com.cn/s/blog_7d95a2e70102v9xr.html,thanks!

关于LayoutInflater.from(context).inflate()的使用的问题相关推荐

  1. Android开发之LayoutInflater.from(context).inflate()方法参数介绍解决RecyclerView加载布局不全的问题

    咱们先看下item的xml布局高度为64dp <?xml version="1.0" encoding="utf-8"?> <Relative ...

  2. 一篇弄懂LayoutInflater.from(context).inflate()

    昨天项目的原因,使用到了这个LayoutInflater.from(context).inflate(),结果发现应该加载的布局没有显示出来.排查了好久发现是照着别人view的时候,直接把Layout ...

  3. Android LayoutInflater.from(context).inflate()方法的作用

    前些天发现了一个蛮有意思的人工智能学习网站,8个字形容一下"通俗易懂,风趣幽默",感觉非常有意思,忍不住分享一下给大家.

  4. LayoutInflater.from(this).inflate()

    LayoutInflater.from(this).inflate() View view = LayoutInflater.from(Context context).inflate(int res ...

  5. View.inflate和LayoutInflater的inflate方法区别

    平时ListView加载item中,adapter的getView方法中,我们经常用到: LayoutInflater.from(mContext).inflate(R.layout.it ,pare ...

  6. LayoutInflater.inflate()方法两个参数和三个参数

    转载请标明出处:https://www.cnblogs.com/tangZH/p/7074853.html 很多人都用过LayoutInflater(布局填充器) 对于我来说通常使用下面两种:Layo ...

  7. Context完全解析

    原文出处:郭霖,http://blog.csdn.net/sinyu890807/article/details/47028975?locationNum=1&fps=1 Context相信所 ...

  8. Android开发知识(二十二)LayoutInflater装载xml布局过程的源码解析

    文章目录 前言 LayoutInflater实例 LayoutInflater的装载过程 include 标签解析 merge 标签解析 attachToRoot参数解析 View创建过程 (1)判断 ...

  9. LayoutInflater的错误用法(Avoid passing null as the view root )

    今天在练习使用Fragment的时候,注意到在使用LayoutInflater的时候有黄色报警(Avoid passing null as the view root (needed to resol ...

最新文章

  1. 突然让我想起了以前的面试(转http://blog.163.com/lzy_1920116/blog/)
  2. IOS atomic与nonatomic,assign,copy与retain的定义和区别
  3. C++中#define用法
  4. POJ 1944 Fiber Communications (枚举 + 并查集 OR 线段树)
  5. python登录网页_Python如何爬取需要登录的页面
  6. springboot热部署之spring-boot-devtools
  7. python中startout是什么意思_Python socket.timeout方法代碼示例
  8. postgres的序列(Sequence)的使用
  9. 如何检查CentOS版本– 8种方法
  10. Machine Learning - XII. Support Vector Machines支持向量机(Week 7)
  11. ArcGIS 设置暂时固定存储地址
  12. 如何下载遥感软件ERDAS
  13. 国产自主可控智慧会议系统解决方案-移动无纸化会议
  14. Error: java.lang.RuntimeException: Some file crunching failed, see logs for details
  15. dB dBm概念及计算
  16. mysql查询字段最大的一条数据类型_SQL查询一个表中类别字段中Max()最大值对应的记录...
  17. Hbuilder和HbuilderX连接夜神模拟器(nox),调试程序
  18. 一键批量下载皮皮虾视频
  19. KB和kb的区别以及我的网速
  20. 如何处理印象笔记安装后图标不显示的情况

热门文章

  1. 220名产品经理快速体验一款app:精选5个好玩的app
  2. java cpu变高,他在做什么
  3. leetcode 左旋转字符串
  4. redis队列生产消费php,redis 队列 生产者 消费者模式
  5. gephi和python_Python爬取B站弹幕+Gephi梳理主线剧情
  6. docker集群(1):docker swarm
  7. java如何输入字符串_JAVA中怎样输入字符串
  8. 学习是我快乐 第十五天
  9. 【源码】网上随意扒的源码
  10. 如何实现chatgpt的打字机效果