android LayoutInflater.inflate()的参数介绍

LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象;

与findViewById区别:

LayoutInflater.inflate是加载一个布局文件;

findViewById则是从布局文件中查找一个控件;

一.获取LayoutInflater对象有三种方法

LayoutInflater inflater=LayoutInflater.from(context);

LayoutInflater inflater=getLayoutInflater();在Activity中可以使用,实际上是View子类下window的一个函数

LayoutInflater inflater=(LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);

二:inflate 参数

public View inflate(int resource, ViewGroup root, boolean attachToRoot) :

reSource:View的layout的ID 
root:需要附加到resource资源文件的根控件,inflate()会返回一个View对象,如果第三个参数attachToRoot为true,就将这个root作为根对象返回,否则仅仅将这个root对象的LayoutParams属性附加到resource对象的根布局对象上,也就是布局文件resource的最外层的View上。如果root为null则会忽略view根对象的LayoutParams属性(注意)。

attachToRoot:是否将root附加到布局文件的根视图上

例子:

LayoutInflater  inflater  = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.fragment, root,false);

或者:

v = inflater.inflate(R.layout.fragment, null);

常见问题:

有时候我们在Adapter加载自定义的view布局文件,布局文件中设置了android:layout_height="100dip",但是运行程序后发现一点作用都没有,相似的还有layout_width等以android:layout_开头的属性设置都没有作用;

因为Adapter里有一个方法是getView,这个返回的VIew是一个从XML布局里加载的,一般如下:

View v = inflater.inflate(R.layout.fragment, null);
 

问题恰恰出在我们的LayoutInflater.from(mContext).inflate(R.layout.main, null);这句代码上,在使用inflate的时候,如果第二个参数(View root)为null,那么将不会加载你的布局文件里的最顶层的那个布局节点的布局相关配置(就是以android:layout_开头的属性)..我们可以看下该方法的实现来说明一下,通过查找源代码,inflate的实现都在这个public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) 方法里定义。。其中一段:

          if (root != null) {
                        if (DEBUG) {
                            System.out.println("Creating params from root: " +
                                    root);
                        }
                        // Create layout params that match root, if supplied
                        params = root.generateLayoutParams(attrs);
                        if (!attachToRoot) {
                            // Set the layout params for temp if we are not
                            // attaching. (If we are, we use addView, below)
                            temp.setLayoutParams(params);
                        }
                    }

代码中可以发现:当root为null的时候是不会执行params = root.generateLayoutParams(attrs);这段代码的,这段代码就是把xml里的布局配置转为LayoutParams,换句说就是加载我们配置的布局属性,以供布局类(FrameLayout等)在onLayout的时候控制View的大小、位置、对齐等等。。以FrameLayout为例,看下它的generateLayoutParams(attrs)方法。

解决办法:

使用LayoutInflate的inflate方法的时候一定要保证root参数不能为null,其实这个root就是父View的意思,就是说你把xml转换为一个VIew的时候,该VIew的Parent是root,如果你不想把该View添加到该root里,那么让第三个参数 attachToRoot为false,如果要添加则为true.

时间: 2024-08-02 13:56:02

android LayoutInflater.inflate()的参数介绍的相关文章

Android LayoutInflater.inflate()的参数及其用法

很多人在网上问LayoutInflater类的用法,以及inflate()方法参数的含义,现解释如下: inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById()的区别,inflate是加载一个布局文件,而findViewById则是从布局文件中查找一个控件. 1.获取LayoutInflater对象有三种方法 LayoutInflater inflater=LayoutInflater.from(this); LayoutInflater inflat

Android LayoutInflater.inflate的使用及源码分析

欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/61913656 本文出自:[余志强的博客] 在实际开发中我们常常需要inflate要给布局然后添加到某个布局容器里面去, 要把xml布局文件转成一个View对象 需要使用LayoutInflater.inflate方法. 在开发中常常使用如下几种方式: inflater.inflate(layoutId, null); inflater.inflate(layoutI

android LayoutInflater.inflate详解

LayoutInflater概述 从XML文件中实例化一个布局成对应的View类, 它从来不会直接使用, 而是使用getLayoutInflater()或者getSystemService(String)来获得一个对应当前context的标准LayoutInflater 实例. 例如: 1 2 3 LayoutInflater inflater = (LayoutInflater)context.getSystemService       (Context.LAYOUT_INFLATER_SE

Android LayoutInflater.inflate使用上的问题解惑

最近在在使用LayoutInflater.inflate方法时遇到了一些问题,以前没有仔细看过此类的使用方法,故将其记录下来,方便日后查阅. 相信大家都知道LayoutInflater.inflate是在android开发遇到的一些使用频率是非常高的方法,如果使用不好的,就会出现一些奇怪的问题. 一个例子如下: 1,一个主布局文件如下 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

Android LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)的参数理解

方法inflate(int resource, ViewGroup root, boolean attachToRoot) 中 第一个参数传入布局的资源ID,生成fragment视图,第二个参数是视图的父视图,通常我们需要父视图来正确配置组件.第三个参数告知布局生成器是否将生成的视图添加给父视图. 我们新建一个项目测试一下: activity_main.xml <?xml version="1.0" encoding="utf-8"?> <Line

【android】LayoutInflater.inflate方法的详解及xml根元素的布局参数不起作用的问题

一.首先看带三个参数的inflate方法: public View inflate (int resource, ViewGroup root, boolean attachToRoot) 1.如果root不为null,且attachToRoot为TRUE,则会在加载的布局文件的最外层再嵌套一层root布局,这时候xml根元素的布局参数当然会起作用. 2.如果root不为null,且attachToRoot为false,则不会在加载的布局文件的最外层再嵌套一层root布局,这个root只会用于为

[Android]LayoutInflater的inflate方法半详解

好久没写博客,作为一名刚加入android阵营的android狗,发心得刷人气来了!!!(半详解是因为说详不那么详,说不详也稍微有点详..)哈哈~~..咳..咳.. 一.Activity中的setContentView 对于刚开始学Android的新手来说,在Activity中加载布局文件的方法是在onCreate()回调方法中直接调用setContentView()方法,如: @Override protected void onCreate(Bundle savedInstanceState

android LayoutInflater和inflate()方法的用法(转载)

原文出处:http://www.cnblogs.com/top5/archive/2012/05/04/2482328.html 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化:而findViewById()是找xml布局文件下的具体widget控件(如Button.TextView等). 具体作用: 1.对于一个没有被载入或者想要动态载入的

Android LayoutInflater.from().inflate()源码解析

我们知道,在Activity#setContentView()中会调用PhoneWindow#setContentView().而在PhoneWindow#setContentView()中有这么一句mLayoutInflater.inflate(layoutResID, mContentParent).这行代码的作用是将我们的activity_main.xml填充到mContentParent中去.详见:setContentView源码解析.在写adapter的时候,也经常写mInflater