当前位置: 首页 > news >正文

android 换肤框架详解2-LayoutInflater源码解析

前面已经讲解了换肤,换资源文件的逻辑,接下来需要讲解一下如何实现自动换肤的逻辑第一步

查看LayoutInflater源码

传送门android 换肤框架详解1-换肤逻辑基本-CSDN博客

要对自动换肤有更深的了解,这里就需要对LayoutInflater源码有所了解

  1. LayoutInflater源码解析

从inflate进来

    public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {return inflate(resource, root, root != null);}
    public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {//这里可以知道,Resources是通过getContext()拿到的,我们可以直接改变getContext()中的Resourcesfinal Resources res = getContext().getResources();if (DEBUG) {Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("+ Integer.toHexString(resource) + ")");}//尝试“预编译”路径(Android 12 以后增加的优化)// 如果系统把这个 layout 提前编译成了预置的 Java 类(precompiled layout),就直接 new 出来返回,不走传统 XML 解析。View view = tryInflatePrecompiled(resource, res, root, attachToRoot);if (view != null) {return view;}//通过 AssetManager 打开一个 XmlResourceParser,指向该 layout xml 文件XmlResourceParser parser = res.getLayout(resource);try {//解析布局return inflate(parser, root, attachToRoot);} finally {parser.close();}}

 public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {synchronized (mConstructorArgs) {Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");final Context inflaterContext = mContext;final AttributeSet attrs = Xml.asAttributeSet(parser);Context lastContext = (Context) mConstructorArgs[0];mConstructorArgs[0] = inflaterContext;View result = root;try {advanceToRootNode(parser);final String name = parser.getName();//解析merge标签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, inflaterContext, attrs, false);} else {// Temp is the root view that was found in the xml//解析xml里面获取到的数据,进行创建Viewfinal View temp = createViewFromTag(root, name, inflaterContext, attrs);ViewGroup.LayoutParams params = null;if (root != null) {if (DEBUG) {System.out.println("Creating params from root: " +root);}// Create layout params that match root, if suppliedparams = root.generateLayoutParams(attrs);if (!attachToRoot) {// Set the layout params for temp if we are not// attaching. (If we are, we use addView, below)temp.setLayoutParams(params);}}if (DEBUG) {System.out.println("-----> start inflating children");}// Inflate all children under temp against its context.rInflateChildren(parser, temp, attrs, true);if (DEBUG) {System.out.println("-----> done inflating children");}// We are supposed to attach all the views we found (int temp)// to root. Do that now.//如果存在VIewGroup,添加View到View上if (root != null && attachToRoot) {root.addView(temp, params);}// Decide whether to return the root that was passed in or the// top view found in xml.if (root == null || !attachToRoot) {result = temp;}}} catch (XmlPullParserException e) {final InflateException ie = new InflateException(e.getMessage(), e);ie.setStackTrace(EMPTY_STACK_TRACE);throw ie;} catch (Exception e) {final InflateException ie = new InflateException(getParserStateDescription(inflaterContext, attrs)+ ": " + e.getMessage(), e);ie.setStackTrace(EMPTY_STACK_TRACE);throw ie;} finally {// Don't retain static reference on context.mConstructorArgs[0] = lastContext;mConstructorArgs[1] = null;Trace.traceEnd(Trace.TRACE_TAG_VIEW);}return result;}}

正在创建View的地方

  private View createViewFromTag(View parent, String name, Context context, AttributeSet attrs) {return createViewFromTag(parent, name, context, attrs, false);}
    View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,boolean ignoreThemeAttr) {try {//通过注册回调创建,这里就是自动将资源文件布局到View上的关键,这里会通过接口回调,回调到自己创建的//监听上,在这个监听里面保存对应的参数和类,然后切换换肤的时候一键修部署到ViewView view = tryCreateView(parent, name, context, attrs);if (view == null) {final Object lastContext = mConstructorArgs[0];mConstructorArgs[0] = context;try {//没有通过监听创建View,走自己的解析方法if (-1 == name.indexOf('.')) {view = onCreateView(context, parent, name, attrs);} else {view = createView(context, name, null, attrs);}} finally {mConstructorArgs[0] = lastContext;}}return view;} catch (InflateException e) {throw e;} }
   public final View tryCreateView(@Nullable View parent, @NonNull String name,@NonNull Context context,@NonNull AttributeSet attrs) {if (name.equals(TAG_1995)) {// Let's party like it's 1995!return new BlinkLayout(context, attrs);}View 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);}return view;}
  public void setFactory2(Factory2 factory) {//在View解析的时候,会先执行这里,如果有创建监听,会执行这里的创建View,我们自定义的这个类,可以不走系统的解析//方法,在自己创建的factory中进行对应解析创建,并且保存对应View在xml中的状态,在换行的时候就可以将保存的类进行//换肤处理if (mFactorySet) {throw new IllegalStateException("A factory has already been set on this LayoutInflater");}if (factory == null) {throw new NullPointerException("Given factory can not be null");}mFactorySet = true;if (mFactory == null) {mFactory = mFactory2 = factory;} else {mFactory = mFactory2 = new FactoryMerger(factory, factory, mFactory, mFactory2);}}

http://www.lryc.cn/news/618032.html

相关文章:

  • 【Linux文件操作】文件操作系统调用
  • 机器学习之DBSCAN
  • Linux中DNS系统搭建与配置指南(配实验步骤与注释)
  • GO学习记录三
  • 【网络运维】Linux:常见 Web 服务器
  • 对自己的 app 进行分析, 诊断,审视
  • FPGA+护理:跨学科发展的探索(二)
  • Python day 41
  • AVS Video Converter视频转换与编辑工具深度评测
  • 什么是电网谐波?
  • PyCharm(2025.1.3.1)绑定 Conda 环境
  • 一篇文章解决Unity没有添加模块选项的问题
  • Android.mk教程
  • 深入解析Windows系统下UDP绑定失败的原理与系统级解决方案
  • Java AI生成长篇小说的实用
  • 算法基础 1
  • 为什么TEXT不区分大小写,而BLOB严格区分?
  • redis笔记(二)
  • OpenBMC中phosphor-dbus-interfaces深度解析:架构、原理与应用实践
  • 02Vue3
  • 贪心----3. 跳跃游戏 II
  • 使用MAS(Microsoft Activation Scripts)永久获得win10专业版和office全套
  • 进程线程切换的区别
  • 【MATLAB 2025a】安装离线帮助文档
  • 第16届蓝桥杯Python青少组中/高级组选拔赛(STEMA)2025年3月9日真题
  • 【k近邻】Kd树的构造与最近邻搜索算法
  • mhal_gpio.c记录gpio相关寄存器和gpio操作函数
  • 【优化】图片批量合并为word
  • 部署open-webui到本地
  • Linux中配置DNS