亲测环境
classpath 'com.android.tools.build:gradle:2.3.1'
compileSdkVersion 23 buildToolsVersion "25.0.0" defaultConfig { minSdkVersion 17 targetSdkVersion 23 versionCode 1 versionName "1.0" vectorDrawables.useSupportLibrary = true }
Activity 继承AppcompatActivity
1.可以正常的使用ImageView
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_wc_black_24dp"
/>
2.但是无法使用
android:background="@drawable/ic_wc_black_24dp"
设置背景,可以在代码中进行设置:
Resources resources = getResources();
Resources.Theme theme = getTheme();
Drawable drawable = VectorDrawableCompat.create(resources, R.drawable.ic_wc_black_24dp, theme);
findViewById(R.id.ttt).setBackground(drawable);
3.在这种情况下是可以使用AppCompatImageView和AppCompatImgaeButton.
<android.support.v7.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_wc_black_24dp"
/>
<android.support.v7.widget.AppCompatImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_wc_black_24dp"
/>
但是仅限于这两种原生控件,在其他非src的场景下,有两种方法可以设置,第一种:就只能按照上述方法在代码中动态的设置。第二种:将矢量图用drawable容器(如StateListDrawable, InsetDrawable, LayerDrawable, LevelListDrawable, 和RotateDrawable)包裹起来使用,否则会在低版本的情况下报错。
Activity没有继承AppCompatActivity时
此时要使用ImageView和ImageButton 就只能使用使用AppCompatImageView和AppCompatImageButton了。使用方法依然同上。其他的情况只适用于代码中获取到Drawable之后再设置的情况。
在该种情况下即使将矢量图包裹到Drawable容器里,也无法使用。ImageView和ImageButton使用srcCompat会被直接忽略掉。
亲测在该环境里没有生成相对应的png图片
听一部分资料说明,会在build里生成相应的png图片,但是在该种环境下,并没有生成相应的图片,怀疑与什么地方的配置有关。