问题
I started using a ContextThemeWrapper
to apply a style dynamically to an ImageButton
; based an answer to another question of mine, and answers to other similar questions.
ContextThemeWrapper wrapper = new ContextThemeWrapper(getContext(), mStyleRes);
mImageButton = new AppCompatImageButton(wrapper, null, 0);
But recently a lint error started appearing on the ContextThemeWrapper
constructor stating:
ContextThemeWrapper
can only be called from within the same library group (groupId=com.android.support)
I noticed that the class marked with the @RestrictTo(LIBRARY_GROUP)
annotation, which is causing the lint error to appear. But I cannot find any information as to why it is restricted to the com.android.support
library group.
As far as I can tell, this is the only way to apply a style, theme or theme overlay to a View
programmatically; other than setting a default style attribute as the third argument in the constructor. So I am wondering why its use would be restricted at all; is there some issue with using the class outside of the support libraries? Could there be side-effects that I am not aware of?
The only similar question that I have come across is one about a (now fixed) bug; that caused this lint error to be displayed on the onCreate
method of a subclass of AppCompatActivity
. I do not think this occurrence is a bug, rather a deliberate restriction; which I would like to know the reasoning behind.
I should note; this restriction (as of now) actually seems to have no effect on the code using a ContextThemeWrapper
. It compiles and runs fine, and works as I would expect it to.
回答1:
android.view.ContextThemeWrapper != android.support.v7.view.ContextThemeWrapper
.
The support library one is annotated @RestrictTo(LIBRARY_GROUP)
, and also @hide
- it's not meant to be a public API.
The first one is public.
来源:https://stackoverflow.com/questions/42814059/why-is-contextthemewrapper-now-restricted-to-the-support-library-group