我使用 Android Studio 2.0 和支持VectorDrawable
库。我的毕业典礼:
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
minSdkVersion 8
vectorDrawables.useSupportLibrary = true
}
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
但是当我尝试在旧设备的菜单中加载图标时:
MenuItem miSubjects = menu.add(R.id.menu_navigation, ACTION_SUBJECTS_ID, order, R.string.action_subjects);
miSubjects.setIcon(ContextCompat.getDrawable(this, R.drawable.ic_labels_gray_24dp));
图标是一个矢量,但 AndroidStudio 必须为旧版本生成和绘制(光栅可绘制)。如果不是这样,我需要如何使用它?
向量示例ic_labels_gray_24dp.xml
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#8a000000"
android:pathData="m15.316,4.5 l-10.316,0.008c-1.169,-0 -2,0.96 -2,2v5.984c0,1.043 0.78,2 2,2l10.316,0.008c0.991,0 0.968,0.014 1.543,-0.6l4.141,-4.4 -4.141,-4.4c-0.594,-0.594 -0.552,-0.6 -1.543,-0.6zM20.019,13.459 L18.316,15.269c-0.284,0.303 -0.614,0.743 -1.354,1.027 -0.741,0.285 -1.161,0.203 -1.646,0.203h-0.002l-10.316,-0.008c-0.74,0 -1.413,-0.225 -1.998,-0.578v1.578c0,1.043 0.78,2 2,2l10.316,0.008c0.991,0 0.968,0.014 1.543,-0.6l4.141,-4.4 -0.98,-1.041z"/>
</vector>
PS它写了一个简单的类。它不能修复 Android Studio 错误,但至少应用程序不会崩溃。也许其他人也有同样的问题。
public final class VectorDrawable {
private VectorDrawable() {}
public static Drawable getDrawable(Context context, final int id) {
return VectorDrawableCompat.create(context.getResources(), id, context.getTheme());
}
}