AppCompat如何膨胀未明确使用AppCompat窗口小部件的布局? [英] How does AppCompat inflate layouts that do not explicitly use AppCompat widgets?

查看:215
本文介绍了AppCompat如何膨胀未明确使用AppCompat窗口小部件的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AppCompat时,我发现我在布局XML文件中使用的是 Button 之类的东西,而不是 android.support.v7.widget.AppCompatButton 之类的东西.我通过 view.getClass().getSimpleName()进行了测试,并确认即使我在XML中将其声明为 Button ,正在加载的类仍在事实 AppCompatButton .

It occurred to me while using AppCompat, that I had been using things like Button instead of android.support.v7.widget.AppCompatButton within my layout XML files. I did a test, via view.getClass().getSimpleName() and confirmed that even though I declared it as a Button in the XML, the class being loaded was in fact AppCompatButton.

在引擎盖下如何工作?

推荐答案

在研究此主题以正确提出问题的过程中,我自己找到了答案.

In the process of researching this topic in order to ask the question correctly, I discovered the answer myself.

使用 AppCompatActivity 时,会发生一些有趣的事情:

When using AppCompatActivity, some interesting things happen:

  1. 通过 setFactory LayoutInflater.Factory 应用于默认的 LayoutInflater .AppCompat中的 AppDelegateImpl 类实现了 Factory 接口,并且根据API级别选择其中一个作为工厂委托.还有一个稍有不同的 Factory2 ,以更高版本的API为目标.
  2. 从XML扩展视图时,视图类的名称将传递到 Factory createView 方法中,该方法可以覆盖创建的实际视图.
  3. 使用 AppCompatViewInflater 中的字符串的硬编码哈希表检查视图的名称,如果找到匹配项,则由委托对视图进行充气,而不是默认的充气器.
  1. A LayoutInflater.Factory is applied to the default LayoutInflater, via setFactory. The AppDelegateImpl classes within AppCompat implement the Factory interface, and one of them is chosen as the factory delegate depending on the API level. There is also a slightly different Factory2, targeting later APIs.
  2. When your views are being inflated from XML, the name of the view's class is passed into the Factory's createView method, which is given the opportunity to override the actual view that is created.
  3. The name of the view is checked against a hard-coded hash table of strings in AppCompatViewInflater, and if a match is found the view is inflated by the delegate, instead of the default inflater.

这篇关于AppCompat如何膨胀未明确使用AppCompat窗口小部件的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆