Android LayoutInflator 解析

一、实际使用场景引入: 

     在ListView的Adapter的getView方法中基本都会出现,使用inflate方法去加载一个布局,用于ListView的每个Item的布局。  同样,在使用ViewPager的PagerAdapter时重载instantiateItem数时经常需要加载一个布局文件。

public Object instantiateItem(ViewGroup container, int position) {
            View view = null;
            if("layout".equals(mContext.getResources().getResourceTypeName(mResIds.get(position)))){
                view = inflater.inflate(mResIds.get(position),null);
            }else {
                ImageView imageView = new ImageView(mContext);
                imageView.setImageResource(mResIds.get(position));
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                view = imageView;
            }
            if(mListener != null){
                mListener.instantiateItem(view,position);
            }
            container.addView(view,new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return view;
        }
 

二、Inflater参数介绍

  使用Inflate时一般有三个参数(int resource, ViewGroup root, boolean attachToRoot)

1)如果inflate(layoutId, null )则layoutId的最外层的控件的宽高是没有效果的

2)如果inflate(layoutId, root, false ) 则认为和上面效果是一样的

3)如果inflate(layoutId, root, true ) 则认为这样的话layoutId的最外层控件的宽高才能正常显示

如果你也这么认为,那么你有就必要好好阅读这篇文章,因为这篇文章首先会验证上面的理解是错误的,然后从源码角度去解释,最后会从ViewGroup与View的角度去解释。

 

实际例子和分析:

http://blog.csdn.net/lmj623565791/article/details/38171465

自己理解:

1)Inflate(resId , null ) 只创建temp ,返回temp。

它调的函数如下:

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
        return inflate(resource, root, root != null);
    }

temp创建过程的源码: 此时的root 为null。

// Temp is the root view that was found in the xml
 final View temp = createViewFromTag(root, name, inflaterContext, attrs);

该View的LayoutParams实现如下:(attr 为加载资源中的  Layout属性设置)

// 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,所以param也无null,所以temp没有自己的Layout属性,保持与父View一致。

如果是ViewGroup 添加一个View而没有设置它的LayoutParam时,默认会有一个params如下:

@Override
   
 protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
       
     return new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
               ViewGroup.LayoutParams.WRAP_CONTENT, 0);
    }

2) Inflate(resId , parent, false )创建temp,然后执行temp.setLayoutParams(params);返回temp

temp创建过程与1)中一致,不同的是此时的 param 即为Inflater出来的资源的自己  layout属性,最终返回temp

3)Inflate(resId , parent, true ) 创建temp,然后执行root.addView(temp, params);最后返回root

 

又把 LayoutInflater 看了一遍。

还可以参考郭师傅的;

 http://blog.csdn.net/guolin_blog/article/details/12921889

 

 

转载于:https://www.cnblogs.com/NeilZhang/p/7786452.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值