问题描述
我开始使用 ContextThemeWrapper
将样式动态地应用于 ImageButton
;基于,以及其他 。
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);
但是最近在 ContextThemeWrapper 构造函数说明:
But recently a lint error started appearing on the
ContextThemeWrapper
constructor stating:
我注意到该类标记为
@RestrictTo(LIBRARY_GROUP)
批注,这会导致出现棉绒错误。但是我找不到关于为什么的任何信息,它仅限于 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.
据我所知,这是将样式,主题或主题叠加层以编程方式应用于
View
的唯一方法;除了将默认样式属性设置为构造函数中的第三个参数之外。所以我想知道为什么它的使用会受到限制。在支持库之外使用类是否有问题?可能会有我不知道的副作用吗?
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?
唯一的是关于一个(现已修复)错误的问题;导致此棉绒错误显示在
AppCompatActivity
子类的 onCreate
方法上。我认为这种情况不是错误,而是有意的限制。我想知道背后的原因。
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.
我应该注意;实际上,使用
ContextThemeWrapper
这种限制(到目前为止)实际上对代码没有任何影响。
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.
推荐答案
!=
android.support.v7.view.ContextThemeWrapper
。
注释了一个支持库
@RestrictTo(LIBRARY_GROUP)
以及 @hide
-并不是要成为公共API。
The support library one is annotated
@RestrictTo(LIBRARY_GROUP)
, and also @hide
- it's not meant to be a public API.
第一个是公开的。
这篇关于为什么现在将ContextThemeWrapper限制为“支持库”组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!