zoukankan      html  css  js  c++  java
  • Android-LayoutInflater

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

    LayoutInflater主要是用于加载布局的,有两个方法可以获取到LayoutInflater的实例:

    第一种,用from方法

    LayoutInflater layoutInflater = LayoutInflater.from(context);  

    第二种,用context的getSystemService方法

    LayoutInflater layoutInflater = (LayoutInflater) context  
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  

    第一种其实是第二种的封装

    获取实例用,就可以调用他的inflate方法加载布局:

    layoutInflater.inflate(resourceId, root);

    inflate()方法一般接收两个参数,第一个参数就是要加载的布局id,第二个参数是指给该布局的外部再嵌套一层父布局,如果不需要就直接传null。这样就成功成功创建了一个布局的实例,之后再将它添加到指定的位置就可以显示出来了。

    比如想要添加一个按钮:

    public class MainActivity extends Activity {  
      
        private LinearLayout mainLayout;  
      
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            mainLayout = (LinearLayout) findViewById(R.id.main_layout);  
            LayoutInflater layoutInflater = LayoutInflater.from(this);  
            View buttonLayout = layoutInflater.inflate(R.layout.button_layout, null);  
            mainLayout.addView(buttonLayout);  
        }  
      
    }  

    Button在界面上显示出来了!说明我们确实是借助LayoutInflater成功将button_layout这个布局添加到LinearLayout中了。LayoutInflater技术广泛应用于需要动态添加View的时候,比如在ScrollView和ListView中,经常都可以看到LayoutInflater的身影。

    源码解析:

    inflate源码:

    public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {  
        synchronized (mConstructorArgs) {  
            final AttributeSet attrs = Xml.asAttributeSet(parser);  
            mConstructorArgs[0] = mContext;  
            View result = root;  
            try {  
                int type;  
                while ((type = parser.next()) != XmlPullParser.START_TAG &&  
                        type != XmlPullParser.END_DOCUMENT) {  
                }  
                if (type != XmlPullParser.START_TAG) {  
                    throw new InflateException(parser.getPositionDescription()  
                            + ": No start tag found!");  
                }  
                final String name = parser.getName();  
                if (TAG_MERGE.equals(name)) {  
                    if (root == null || !attachToRoot) {  
                        throw new InflateException("merge can be used only with a valid "  
                                + "ViewGroup root and attachToRoot=true");  
                    }  
                    rInflate(parser, root, attrs);  
                } else {  
                    View temp = createViewFromTag(name, attrs);  
                    ViewGroup.LayoutParams params = null;  
                    if (root != null) {  
                        params = root.generateLayoutParams(attrs);  
                        if (!attachToRoot) {  
                            temp.setLayoutParams(params);  
                        }  
                    }  
                    rInflate(parser, temp, attrs);  
                    if (root != null && attachToRoot) {  
                        root.addView(temp, params);  
                    }  
                    if (root == null || !attachToRoot) {  
                        result = temp;  
                    }  
                }  
            } catch (XmlPullParserException e) {  
                InflateException ex = new InflateException(e.getMessage());  
                ex.initCause(e);  
                throw ex;  
            } catch (IOException e) {  
                InflateException ex = new InflateException(  
                        parser.getPositionDescription()  
                        + ": " + e.getMessage());  
                ex.initCause(e);  
                throw ex;  
            }  
            return result;  
        }  
    } 

    可以看到,inflate是用pull来解析xml格式的,其中调用了createViewFromTag()这个方法,并把节点名和参数传了进去。看到这个方法名,我们就应该能猜到,它是用于根据节点名来创建View对象的。确实如此,在createViewFromTag()方法的内部又会去调用createView()方法,然后使用反射的方式创建出View的实例并返回。

    这里创建的是根布局的实例,接下来会调用rInflate()方法来循环遍历这个根布局下的子元素:

    private void rInflate(XmlPullParser parser, View parent, final AttributeSet attrs)  
            throws XmlPullParserException, IOException {  
        final int depth = parser.getDepth();  
        int type;  
        while (((type = parser.next()) != XmlPullParser.END_TAG ||  
                parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {  
            if (type != XmlPullParser.START_TAG) {  
                continue;  
            }  
            final String name = parser.getName();  
            if (TAG_REQUEST_FOCUS.equals(name)) {  
                parseRequestFocus(parser, parent);  
            } else if (TAG_INCLUDE.equals(name)) {  
                if (parser.getDepth() == 0) {  
                    throw new InflateException("<include /> cannot be the root element");  
                }  
                parseInclude(parser, parent, attrs);  
            } else if (TAG_MERGE.equals(name)) {  
                throw new InflateException("<merge /> must be the root element");  
            } else {  
                final View view = createViewFromTag(name, attrs);  
                final ViewGroup viewGroup = (ViewGroup) parent;  
                final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);  
                rInflate(parser, view, attrs);  
                viewGroup.addView(view, params);  
            }  
        }  
        parent.onFinishInflate();  
    }

    在这里同样是createViewFromTag()方法来创建View的实例,然后还会递归调用rInflate()方法来查找这个View下的子元素,每次递归完成后则将这个View添加到父布局当中。

    这样的话,把整个布局文件都解析完成后就形成了一个完整的DOM结构,最终会把最顶层的根布局返回,至此inflate()过程全部结束。

    public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)的第三个参数attachToRoot

    1. 如果root为null,attachToRoot将失去作用,设置任何值都没有意义。

    2. 如果root不为null,attachToRoot设为true,则会给加载的布局文件的指定一个父布局,即root。

    3. 如果root不为null,attachToRoot设为false,则会将布局文件最外层的所有layout属性进行设置,当该view被添加到父view当中时,这些layout属性会自动生效。

    4. 在不设置attachToRoot参数的情况下,如果root不为null,attachToRoot参数默认为true。

    所有控件的layout_width等属性,都要有一个父布局才能生效,因为这是用来设置view在布局中的大小,而不是view的大小,所以叫layout_width,不是width。

    而LinearLayout的layout_width有效是因为,在setContentView()方法中,Android会自动在布局文件的最外层再嵌套一个FrameLayout,id为content

    所以叫setContentView(),其实这个方法也是用Layoutlnflater()实现的

  • 相关阅读:
    van Emda Boas
    斐波那契堆
    NTT
    FFT
    KDTree
    扩展kmp
    kmp
    Dancing Links
    树的prufer编码
    有向图最小路径覆盖
  • 原文地址:https://www.cnblogs.com/qlky/p/5674975.html
Copyright © 2011-2022 走看看