前面我们对setContentView的源码进行了深入的分析Android最全的setContentView源码分析,那了解了View的创建流程,我们可以做些什么呢?答案就是我们可以通过拦截View的创建流程去解析View对应的属性(如textColor、src、background等),然后进行APP的换肤!
前面我们分析过View的创建流程会交给Factory2.onCreateView()
方法去实现,那我们就通过实现 LayoutInflater.factory2接口
进行View的创建拦截!
LayoutInflater.factory2
接口class BaseSkinActivity : BaseActivity(), LayoutInflater.Factory2
onCreate(savedInstanceState: Bundle?)
中设置LayoutInflater.factory2 override fun onCreate(savedInstanceState: Bundle?) {val layoutInflater = LayoutInflater.from(this)layoutInflater.factory2 = this //当前activity实现 LayoutInflater.factory2接口super.onCreate(savedInstanceState)}
在factory2接口方法中进行View的创建拦截处理
override fun onCreateView(parent: View?,name: String,context: Context,attrs: AttributeSet): View? {val view = createView(parent, name, context, attrs)if (view != null) {//在这里可以通过attrs解析View的属性和值,进行对应的换肤操作}return view}private fun createView(parent: View?,name: String,context: Context,attrs: AttributeSet): View {//这里的实现参考系统中的AppCompatDelegateImpl.createView方法,直接抄if (appCompatViewInflater == null) {appCompatViewInflater = AppCompatViewInflater()}var inheritContext = falseif (IS_PRE_LOLLIPOP) {if (mLayoutIncludeDetector == null) {mLayoutIncludeDetector = LayoutIncludeDetector()}inheritContext = if (mLayoutIncludeDetector!!.detect(attrs)) {true} else {if (attrs is XmlPullParser // If we have a XmlPullParser, we can detect where we are in the layout) (attrs as XmlPullParser).depth > 1 // Otherwise we have to use the old heuristicelse shouldInheritContext(parent as ViewParent?)}}return appCompatViewInflater!!.createView(parent, name, context, attrs, inheritContext,IS_PRE_LOLLIPOP, /* 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 */)}private fun shouldInheritContext(parent: ViewParent?): Boolean {var parent: ViewParent? = parentval windowDecor: View = window.decorViewwhile (true) {if (parent == null) {return true} else if (parent === windowDecor || parent !is View|| ViewCompat.isAttachedToWindow((parent as View?)!!)) {return false}parent = parent.getParent()}}
其中AppCompatViewInflater
我们无法调用使用系统对应的createView
方法,最简单的方式是我们直接复制一份相同的AppCompatViewInflater
类,去设置createView
为public
方法即可,createView
方法的实现完全可以参考系统中的AppCompatDelegateImpl.createView
方法~~
这里只是简单的提供了APP插件换肤的思路,实际开发中插件换肤需要考虑的问题是比较多的,如多套皮肤的管理、自定义View的处理等等问题,但插件换肤的核心就是Hook View的创建流程,从而替换View的属性,说到底还是对源码的深刻理解。
如果以上文章对您有一点点帮助,希望您不要吝啬的点个赞加个关注,您每一次小小的举动都是我坚持写作的不懈动力!ღ( ´・ᴗ・` )