美文网首页Android开发
关于LayoutInflater

关于LayoutInflater

作者: hxg_ | 来源:发表于2016-05-10 16:28 被阅读78次

用途

Instantiates a layout XML file into its corresponding {@link android.view.View}* objects.
实例化一个xml布局文件到对应的View对象上。

使用

不应该直接使用,而是使用Activity.getLayoutInflater()或者是Context类的getSystemService()来或许实例:

LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE)
LayoutInflater inflater = mActivity.getLayoutInflater();

我们平时使用的LayoutInflater.from(mContext),在内部的调用就是通过第一种方式进行实例化的:

/** * Obtains the LayoutInflater from the given context. */
public static LayoutInflater from(Context context) {    
  LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
  if (LayoutInflater == null) {        
    throw new AssertionError("LayoutInflater not found.");    }   
  return LayoutInflater;}

Filter

可以自定义Filter,过滤自己不想让inflate的类:

if (mFilter != null && clazz != null) {    
  boolean allowed = mFilter.onLoadClass(clazz);    
  if (!allowed) {        
    failNotAllowed(name, prefix, attrs);    
    }
 }

如果不允许Load,就会抛出异常:

private void failNotAllowed(String name, String prefix, AttributeSet attrs) {    
  throw new InflateException(attrs.getPositionDescription()+ ": Class not allowed to be inflated " + (prefix != null ? (prefix + name) : name));
}

关键方法:inflate

LayoutInflater类中提供了2个inflate的方法:

inflate(int resource, ViewGroup root)
inflate(int resource, ViewGroup root, boolean attachToRoot)

其中,第一个方法直接调用了三个参数的方法:

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

之后,调用的方法,相当于用xml解析器,解析当前的xml布局,关键代码部分:

final View temp = createViewFromTag(root, name, attrs, false);
ViewGroup.LayoutParams params = null;
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);    
     }
    }
    if (DEBUG) {    System.out.println("-----> start inflating children");}
    // Inflate all children under temprInflate(parser, temp, attrs, true, true);
    if (DEBUG) {    System.out.println("-----> done inflating children");}
    // We are supposed to attach all the views we found (int temp)
    // to root. Do that now.
    if (root != null && attachToRoot) {   
       root.addView(temp, params);
    }
    // Decide whether to return the root that was passed in or the
    // top       view found in xml.
    if (root == null || !attachToRoot) {    result = temp;}

首先床创建了一个名为temp的View;
其次,获得来xml的layout params

params = root.generateLayoutParams(attrs);

如果rootView!=null && !attachToRoot,就为temp设置params;
如果rootView!=null && attachToRoot,就把temp add到rootView中;
如果root == null || !attachToRoot ,将temp赋值给result;
结论:
inflate(resId , null) 只创建temp ,返回temp
inflate(resId , parent, false)创建temp,然后执行设置params,返回temp
inflate(resId , parent, true) 创建temp,然后执行root.addView(temp, params);最后返回root
因此,通过inflate(resId , null )方法得到的布局,是不会获得xml中正常的宽高等layout params;
通过其他两种方法均可获得;
另外,在listView中,如果用inflate(resId , parent, false),在listView的adapter中来获取convertView的话,会报错:

java.lang.UnsupportedOperationException:   
addView(View, LayoutParams) is not supported in AdapterView 

因为在adapterView的源码中,不允许进行addView的操作:

public void addView(View child) {  
        throw new UnsupportedOperationException("addView(View) is not supported in AdapterView");  
} 

相关文章

网友评论

    本文标题:关于LayoutInflater

    本文链接:https://www.haomeiwen.com/subject/tidyrttx.html