问题
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.
How does this work, under the hood?
回答1:
In the process of researching this topic in order to ask the question correctly, I discovered the answer myself.
When using AppCompatActivity, some interesting things happen:
- A
LayoutInflater.Factoryis applied to the defaultLayoutInflater, viasetFactory. TheAppDelegateImplclasses within AppCompat implement theFactoryinterface, 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'screateViewmethod, 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.
来源:https://stackoverflow.com/questions/54575093/how-does-appcompat-inflate-layouts-that-do-not-explicitly-use-appcompat-widgets