API 21级之前如何在代码中获取主题drawable?
How to obtain a themed drawable in code before API Level 21?
我想使用 Context
或 Resources
来获得主题绘图,而我的 minSDK
是 15。我该怎么做呢?
developer documentation for Resources 有一条注释说,
Note: To obtain a themed drawable, use Context.getDrawable(int) or getDrawable(int, Theme) passing the desired theme.
Context.getDrawable(int)
和 Resources.getDrawable(int, Theme)
都是在 API 21 中添加的,所以我不能使用它们。如果我查看 [ResourcesCompat 文档](http://developer.android.com/reference/android/support/v4/content/res/ResourcesCompat.html#getDrawable(android.content.res.Resources, int, android.content.res.Resources.Theme)),它有类似的限制:
Prior to API level 21, the theme will not be applied and this method simply calls through to getDrawable(int).
真的没有办法在API 21级之前获得主题画吗?
如果需要,我可以提供示例代码,但它实际上只是一个指定属性的 attrs.xml
,定义该属性的 themes.xml
,应用它的 ContextThemeWrapper
,以及 drawable/foobar.xml
使用 <solid android:color="?attr/MY_ATTRIBUTE" />
。
发布问题后,我发现在 API 级别 21 之前,您不能在可绘制 XML 文件中使用 ?attr/
。有关详细信息,请参阅 How to reference style attributes from a drawable? .
我想使用 Context
或 Resources
来获得主题绘图,而我的 minSDK
是 15。我该怎么做呢?
developer documentation for Resources 有一条注释说,
Note: To obtain a themed drawable, use Context.getDrawable(int) or getDrawable(int, Theme) passing the desired theme.
Context.getDrawable(int)
和 Resources.getDrawable(int, Theme)
都是在 API 21 中添加的,所以我不能使用它们。如果我查看 [ResourcesCompat 文档](http://developer.android.com/reference/android/support/v4/content/res/ResourcesCompat.html#getDrawable(android.content.res.Resources, int, android.content.res.Resources.Theme)),它有类似的限制:
Prior to API level 21, the theme will not be applied and this method simply calls through to getDrawable(int).
真的没有办法在API 21级之前获得主题画吗?
如果需要,我可以提供示例代码,但它实际上只是一个指定属性的 attrs.xml
,定义该属性的 themes.xml
,应用它的 ContextThemeWrapper
,以及 drawable/foobar.xml
使用 <solid android:color="?attr/MY_ATTRIBUTE" />
。
发布问题后,我发现在 API 级别 21 之前,您不能在可绘制 XML 文件中使用 ?attr/
。有关详细信息,请参阅 How to reference style attributes from a drawable? .