【Android源码】不同版本系统如何加载View AppCompatActivity和Activity的setContentView...

AppCompatActivity是support.v7包里面用来替代Activity的Activity组件,主要用来兼容5.0之后的新特性,当我们在不同版本的系统里面使用的时候会发现,在5.0之后的系统中,控件会带新特性,而在之前的版本中,则使用的控件则不能使用新特新。

比如: TextView在Activity下是android.widget.TextView, 而在AppCompatActivity中则是android.support.v7.widget.AppCompatTextView, 为什么在AppCompatActivity中,我们的TextView会被系统篡改成AppCompatTextView,今天我们来一起找找原因。

首先我们来看下AppCompatActivity是如何加载布局的:

// AppCompatActivity.java
@Override
public void setContentView(@LayoutRes int layoutResID) {
   getDelegate().setContentView(layoutResID);
}

@NonNull
public AppCompatDelegate getDelegate() {
   if (mDelegate == null) {
       mDelegate = AppCompatDelegate.create(this, this);
   }
   return mDelegate;
}

private static AppCompatDelegate create(Context context, Window window,
       AppCompatCallback callback) {
   final int sdk = Build.VERSION.SDK_INT;
   if (BuildCompat.isAtLeastN()) {
       return new AppCompatDelegateImplN(context, window, callback);
   } else if (sdk >= 23) {
       return new AppCompatDelegateImplV23(context, window, callback);
   } else if (sdk >= 14) {
       return new AppCompatDelegateImplV14(context, window, callback);
   } else if (sdk >= 11) {
       return new AppCompatDelegateImplV11(context, window, callback);
   } else {
       return new AppCompatDelegateImplV9(context, window, callback);
   }
}
复制代码

可以发现在create方法中,对版本进行了控制,对于不同的版本构建了不同的APPCOMPatDelegate对象。当我们点进去会发现,这些根据版本构建的对象最终都是继承自同一个类AppCompatDelegateImplV9,而AppCompatDelegateImplV9则是实现了一个LayoutInflaterFactory的接口。这个接口到底是什么东西呢?

我们暂时先不要管这个,接着向下看:

@Override
public void setContentView(View v) {
   ensureSubDecor();
   ViewGroup contentParent = (ViewGroup) mSubDecor.findViewById(android.R.id.content);
   contentParent.removeAllViews();
   contentParent.addView(v);
   mOriginalWindowCallback.onContentChanged();
}

private ViewGroup createSubDecor() {
    subDecor = (ViewGroup) inflater.inflate(R.layout.abc_screen_simple, null);
    final ContentFrameLayout contentView = (ContentFrameLayout) subDecor.findViewById(
                R.id.action_bar_activity_content);
}
复制代码

这边其实就和Activity加载布局是一样的了,都是通过先添加DecorView,在找到名为content的framelayout的id,通过在这个id上添加我们的布局:

请参考 【Android源码】Activity如何加载布局

这个时候我们就会发现在我们当时做的【Android源码】LayoutInflater 分析【Android源码】View的创建流程,所没有注意到的细节:

View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
        boolean ignoreThemeAttr) {
      try {
      View view;
      if (mFactory2 != null) {
          view = mFactory2.onCreateView(parent, name, context, attrs);
      } else if (mFactory != null) {
          view = mFactory.onCreateView(name, context, attrs);
      } else {
          view = null;
      }
    
      if (view == null && mPrivateFactory != null) {
          view = mPrivateFactory.onCreateView(parent, name, context, attrs);
      }
    
      if (view == null) {
          final Object lastContext = mConstructorArgs[0];
          mConstructorArgs[0] = context;
          try {
              if (-1 == name.indexOf('.')) {
                  view = onCreateView(parent, name, attrs);
              } else {
                  view = createView(name, null, attrs);
              }
          } finally {
              mConstructorArgs[0] = lastContext;
          }
      }
    
      return view;
    } catch (InflateException e) {
      throw e;
    }      
}
复制代码

createViewFromTag是用来创建View的,但是我们会发现在代码中会有factory2、factory这两个东西,而这两个东西就是我们在上面发现的AppCompatDelegateImplV9实现的LayoutInflaterFactory接口,只要实现了这个接口,那么View会被这个接口来赋值,而不是接着走原来的View的创建过程。

所以我们现在返回到AppCompatDelegateImplV9中,找到接口的实现方法:

@Override
public final View onCreateView(View parent, String name,
       Context context, AttributeSet attrs) {
   // First let the Activity's Factory try and inflate the view
   final View view = callActivityOnCreateView(parent, name, context, attrs);
   if (view != null) {
       return view;
   }

   // If the Factory didn't handle it, let our createView() method try
   return createView(parent, name, context, attrs);
}

@Override
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 its context if we're running pre-v21
   final boolean inheritContext = isPre21 && shouldInheritContext((ViewParent) 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 */
           VectorEnabledTintResources.shouldBeUsed() /* Only tint wrap the context if enabled */
   );
}
复制代码

在这个实现方法中,最终我们找到类AppCompatViewInflater,而这个类通过createView方法来直接赋值给View:

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

   View view = null;

   // We need to 'inject' our tint aware Views in place of the standard framework versions
   switch (name) {
       case "TextView":
           view = new AppCompatTextView(context, attrs);
           break;
      case "ImageView":
           view = new AppCompatImageView(context, attrs);
           break;
      // 代码省略
   }
   return view;
}
复制代码

可以发现,如果是TextView,就直接返回V7包中的AppCompatTextView类,所以这就是为什么当我们使用AppCompatActivity的时候,TextView变成AppCompatTextView的原因。

通过实现LayoutInflaterFactory,系统就会根据当前系统的版本号,来对应的生成View,所以这就是为什么在不同版本中,同一个控件会有不同的效果的原因。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值