/** Override onCreateView to instantiate names that correspond to the widgets known to the Widget factory. If we don't find a match, call through to our super class. */ @Override protected View onCreateView(String name, AttributeSet attrs) throws ClassNotFoundException { for (String prefix : sClassPrefixList) { try { View view = createView(name, prefix, attrs); if (view != null) { return view; } } catch (ClassNotFoundException e) { // In this case we want to let the base class take a crack // at it. } }
/** * Inflate a new view hierarchy from the specified xml resource. Throws * {@link InflateException} if there is an error. * * @param resource ID for an XML layout resource to load (e.g., * <code>R.layout.main_page</code>) * @param root Optional view to be the parent of the generated hierarchy. * @return The root View of the inflated hierarchy. If root was supplied, * this is the root View; otherwise it is the root of the inflated * XML file. */ public View inflate(@LayoutRes int resource, @Nullable ViewGroup root){ return inflate(resource, root, root != null); }
/** * Inflate a new view hierarchy from the specified xml resource. Throws * {@link InflateException} if there is an error. * * @param resource ID for an XML layout resource to load (e.g., * <code>R.layout.main_page</code>) * @param root Optional view to be the parent of the generated hierarchy (if * <em>attachToRoot</em> is true), or else simply an object that * provides a set of LayoutParams values for root of the returned * hierarchy (if <em>attachToRoot</em> is false.) * @param attachToRoot Whether the inflated hierarchy should be attached to * the root parameter? If false, root is only used to create the * correct subclass of LayoutParams for the root view in the XML. * @return The root View of the inflated hierarchy. If root was supplied and * attachToRoot is true, this is root; otherwise it is the root of * the inflated XML file. */ public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot){ final Resources res = getContext().getResources();
if (root != null) { //可以看到,只有root即我们指定的parent不为null的时候,才会使用到根布局中指定的布局参数。**这里就解释了为什么③④⑥中我们的高宽值没有生效** params = root.generateLayoutParams(attrs); if (!attachToRoot) { //attachToRoot为false时,才会给我们xml中的跟布局设置布局参数,**这里解释了为什么①的效果跟我们预期相同** // Set the layout params for temp if we are not // attaching. (If we are, we use addView, below) temp.setLayoutParams(params); } }
// Inflate all children under temp against its context. //这里递归调用,解析temp的所有子控件并把解析的子控件添加到temp中 rInflateChildren(parser, temp, attrs, true);
// We are supposed to attach all the views we found (int temp) // to root. Do that now. if (root != null && attachToRoot) { //这里可以看到,如果root即parent不为null且attachToRoot为true的话,就把我们xml解析出来的布局添加到root中 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) { //这里可以看到,如果root即parent为null或者attachToRoot为false,就直接把xml的跟布局返回。也就是把xml中的根布局作为根布局 result = temp; } }
/** * This method is not supported and throws an UnsupportedOperationException when called. * * @param child Ignored. * @param index Ignored. * * @throws UnsupportedOperationException Every time this method is invoked. */ @Override publicvoidaddView(View child, int index){ thrownew UnsupportedOperationException("addView(View, int) is not supported in AdapterView"); }