1

我还想在 API 级别 21 之前使用可缩放矢量图形重构我的应用程序(因为支持库 23.2 是可能的,这是一个很酷的工作人员),而不是生成 png-s。我唯一的问题是,我不能让它与 StateListDrawables 一起工作。在重构之前,我使用过 StateListDrawables,例如:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/dashboard_reset_max_alt_disabled" />
    <item android:state_pressed="true" android:drawable="@drawable/dashboard_reset_max_alt_pressed"/>
    <item android:drawable="@drawable/dashboard_reset_max_alt"/>
</selector>

其中“ dashboard_reset_max_alt*”是 svg-s。

但是,如果我vectorDrawables.useSupportLibrary = true在 build.gradle 中设置了“”,我既不能在app:srcCompat属性中也不能在getDrawable()方法中使用它们。你知道使用我已经拥有的 xml 文件和 svg-s 的解决方案吗?

崩溃日志:

android.content.res.Resources$NotFoundException: File res/drawable/computer_cog_20dp.xml from drawable resource ID #0x7f02016c
at android.content.res.Resources.loadDrawable(Resources.java:3063)
at android.content.res.Resources.getDrawable(Resources.java:1624)
at com.myapp.helper.SkinHelper.getDrawable(SkinHelper.java:426)
...

getDrawable 方法如下所示:

Drawable d;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    d = c.getDrawable(resId);
else d = c.getResources().getDrawable(resId);
// Wrap the drawable so that future tinting calls work
// on pre-v21 devices. Always use the returned drawable.
return DrawableCompat.wrap(d);

我知道我可以以编程方式创建 StateListDrawables,但我更喜欢一种解决方法,这样我就可以使用我已经拥有的 xml-s。你有什么想法?

4

0 回答 0