Why is ContextThemeWrapper now restricted to the Support Library group?

巧了我就是萌 提交于 2020-04-08 18:49:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!