AppCompat如何膨胀未明确使用AppCompat窗口小部件的布局? [英] How does AppCompat inflate layouts that do not explicitly use AppCompat widgets?
问题描述
使用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:
- 通过
setFactory
将LayoutInflater.Factory
应用于默认的LayoutInflater
.AppCompat中的AppDelegateImpl
类实现了Factory
接口,并且根据API级别选择其中一个作为工厂委托.还有一个稍有不同的Factory2
,以更高版本的API为目标. - 从XML扩展视图时,视图类的名称将传递到
Factory
的createView
方法中,该方法可以覆盖创建的实际视图. - 使用
AppCompatViewInflater
中的字符串的硬编码哈希表检查视图的名称,如果找到匹配项,则由委托对视图进行充气,而不是默认的充气器.
- A
LayoutInflater.Factory
is applied to the defaultLayoutInflater
, viasetFactory
. TheAppDelegateImpl
classes within AppCompat implement theFactory
interface, and one of them is chosen as the factory delegate depending on the API level. There is also a slightly differentFactory2
, targeting later APIs. - When your views are being inflated from XML, the name of the view's class is passed into the
Factory
'screateView
method, which is given the opportunity to override the actual view that is created. - 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屋!