将最新的支持库添加到您的应用程序中build.gradle
依赖项:
compile 'com.android.support:appcompat-v7:26.0.2'
并在同一文件中添加以下行:
android {
...
defaultConfig {
...
vectorDrawables.useSupportLibrary = true
}
...
}
通过导入矢量图像矢量资产工作室.
就这样,您准备好了!
图像视图
XML
Use app:srcCompat
属性而不是android:src
:
<ImageView
...
app:srcCompat="@drawable/your_vector"
... />
以编程方式
直接来自资源 ID:
imageView.setImageResource(R.drawable.your_drawable);
Set as Drawable
对象(例如用于着色):
Drawable vectorDrawable
= AppCompatResources.getDrawable(context, R.drawable.your_vector);
imageView.setImageDrawable(vectorDrawable);
如果你想设置色调:
DrawableCompat.setTint
(vectorDrawable, ContextCompat.getColor(context, R.color.your_color));
可绘制的文本视图
XML
没有简单的解决方案:XML 属性android:drawableTop(Bottom etc)
无法处理 Lollipop 之前的矢量图像。一种解决方案是将初始化块添加到 Activity 并将向量包装到另一个 XML 可绘制对象中。第二 -定义自定义 TextView.
以编程方式
直接设置资源是不行的,必须使用Drawable
目的。以与以下相同的方式获取ImageView
并使用适当的方法设置它:
textView.setCompoundDrawablesWithIntrinsicBounds(vectorDrawable, null, null, null);
菜单图标
没有什么特别的:
<item
...
android:icon="@drawable/your_vector"
... />
menuItem.setIcon(R.drawable.your_vector);
通知:
It's 不可能的,你必须使用 PNG :(