美文网首页view绘制
LayoutInflater.createViewFromTag

LayoutInflater.createViewFromTag

作者: 与猎犬赛跑的野兔 | 来源:发表于2017-12-04 16:34 被阅读0次

    当LayoutInflater.inflate(context,resId,attachedRoot)中会调用createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
    boolean ignoreThemeAttr)

     /**
         * Creates a view from a tag name using the supplied attribute set.
         * <p>
         * <strong>Note:</strong> Default visibility so the BridgeInflater can
         * override it.
         *
         * @param parent the parent view, used to inflate layout params
         * @param name the name of the XML tag used to define the view
         * @param context the inflation context for the view, typically the
         *                {@code parent} or base layout inflater context
         * @param attrs the attribute set for the XML tag used to define the view
         * @param ignoreThemeAttr {@code true} to ignore the {@code android:theme}
         *                        attribute (if set) for the view being inflated,
         *                        {@code false} otherwise
         */
        View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
                boolean ignoreThemeAttr) {
            if (name.equals("view")) {                                                // tag.1
                name = attrs.getAttributeValue(null, "class");                        // tag.2
            }
    
            // Apply a theme wrapper, if allowed and one is specified.
            if (!ignoreThemeAttr) {
                final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);
                final int themeResId = ta.getResourceId(0, 0);
                if (themeResId != 0) {
                    context = new ContextThemeWrapper(context, themeResId);
                }
                ta.recycle();
            }
    
            if (name.equals(TAG_1995)) {   //throw exception
                // Let's party like it's 1995!
                return new BlinkLayout(context, attrs);    
            }
            ....
        }
    

    name.equals(TAG_1995) 这里name = null。
    从代码里面的线索是:
    tag.1 这里返回的是 true ,即name = “view”
    tag.2 这里就返回了null

    那么 name 为什么等于“view”?
    看了一下代码,才发现自己的错误:

    <view     // 这里错了
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#E5E5E5"
        />
    

    那这里的view代表什么呢?
    对于一个自定义View来说,在XML里面有两种写法:

    <view
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        class="com.your.package.YourCustomView" />  // Android Studio 3.0提示没有class 这个属性名,应该是不建议用了吧
    
    <com.your.package.YourCustomView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    

    也就是说对于一个自定义Widget组件,name = 最开头写的字段。自己手误把View 写成 view 没想到还有这个问题,记录一下。

    相关文章

      网友评论

        本文标题:LayoutInflater.createViewFromTag

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