Android 探究 LayoutInflater setFactory文章的补充

原创 2016年05月30日 11:35:56
    compile ''
    compile ''


    public View createView(View parent, final String name, @NonNull Context context,
            @NonNull AttributeSet attrs) {
        final boolean isPre21 = Build.VERSION.SDK_INT < 21;

        if (mAppCompatViewInflater == null) {
            mAppCompatViewInflater = new AppCompatViewInflater();

        // We only want the View to inherit it's context if we're running pre-v21 and...
        final boolean inheritContext = isPre21 && mSubDecorInstalled && parent != null
                // We do not want to inherit context from any decor content
                && parent.getId() !=
                // We do not want to inherit context if this is the root view in the layout.
                // We use parent.isAttachedToWindow() to determine this, which works because
                // an inflated layout is only added to the hierarchy AFTER it is completely
                // inflated. Thus isAttachedToWindow() will only return true if the parent
                // has not been inflated within the outer inflation call.
                && !ViewCompat.isAttachedToWindow(parent);

        return mAppCompatViewInflater.createView(parent, name, context, attrs, inheritContext,
                isPre21, /* Only read android:theme pre-L (L+ handles this anyway) */
                true /* Read read app:theme as a fallback at all times for legacy reasons */


    public final View createView(View parent, final String name, @NonNull Context context,
            @NonNull AttributeSet attrs, boolean inheritContext,
            boolean readAndroidTheme, boolean readAppTheme) {
        final Context originalContext = context;

        // We can emulate Lollipop's android:theme attribute propagating down the view hierarchy
        // by using the parent's context
        if (inheritContext && parent != null) {
            context = parent.getContext();
        if (readAndroidTheme || readAppTheme) {
            // We then apply the theme on the context, if specified
            context = themifyContext(context, attrs, readAndroidTheme, readAppTheme);

        // We need to 'inject' our tint aware Views in place of the standard framework versions
        switch (name) {
            case "EditText":
                return new AppCompatEditText(context, attrs);
            case "Spinner":
                return new AppCompatSpinner(context, attrs);
            case "CheckBox":
                return new AppCompatCheckBox(context, attrs);
            case "RadioButton":
                return new AppCompatRadioButton(context, attrs);
            case "CheckedTextView":
                return new AppCompatCheckedTextView(context, attrs);
            case "AutoCompleteTextView":
                return new AppCompatAutoCompleteTextView(context, attrs);
            case "MultiAutoCompleteTextView":
                return new AppCompatMultiAutoCompleteTextView(context, attrs);
            case "RatingBar":
                return new AppCompatRatingBar(context, attrs);
            case "Button":
                return new AppCompatButton(context, attrs);
            case "TextView":
                return new AppCompatTextView(context, attrs);

        if (originalContext != context) {
            // If the original context does not equal our themed context, then we need to manually
            // inflate it using the name so that android:theme takes effect.
            return createViewFromTag(context, name, attrs);

        return null;


    compile ''
    compile ''


Android 探究 LayoutInflater setFactory

转自: 1、概述 对于LayoutInflater setFac...

Android 探究 LayoutInflater setFactory

转载请标明出处:; 本文出自:【张鸿洋的博客】 1、概述对于Layou...

Android探究之LayoutInflater setFactory

转载自: 1、概述 对于LayoutInflater setFactor...


  • zhongwn
  • zhongwn
  • 2016年10月29日 18:52
  • 812

探究 LayoutInflater setFactory

1 概述    对于LayoutInflater setFactory,平时我们很少用到这个API,但是这个API我觉得还是有学习的必要的,能够很多意象不到的问题,准备围绕这方面编写一系列的文章。 ...
  • fwt336
  • fwt336
  • 2016年11月21日 13:44
  • 96

Android 中LayoutInflater的使用

  • 2010年11月15日 11:10
  • 32KB
  • 下载

Android LayoutInflater.Factory主题切换

  • 2016年05月13日 11:05
  • 360KB
  • 下载

Android LayoutInflater(布局加载器)详解

  • 2016年03月23日 16:17
  • 700

Android LayoutInflater原理分析,带你一步步深入了解View(一)

转载请注明出处: 有段时间没写博客了,感觉都有些生疏了呢。最近繁忙的工作终于告一...
您举报文章:Android 探究 LayoutInflater setFactory文章的补充