问题
Im trying to use vector drawables on pre lollipop devices. I did all as instructed here but i still get this crash.
build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-beta6'
}
}
apply plugin: 'com.android.application'
repositories {
maven { url 'http://maven.android-forever.com' }
jcenter()
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.test.app"
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
compile "de.greenrobot:eventbus:2.4.0"
compile 'de.greenrobot:greendao:2.1.0'
compile "com.af:android-utility:1.0.0.9"
compile project(':volleyplus')
compile project (':libvlc')
}
triangle.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/triangle_v"/>
</selector>
triangle_v.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="100dp"
android:width="100dp"
android:viewportHeight="100"
android:viewportWidth="100">
<path
android:name="triangle"
android:fillColor="#FF0000"
android:pathData="m 50,0 l 50,100 -100,0 z"/>
</vector>
layout.xml
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/triangle"/>
I also tried app:srcCompat
and in that case, drawable just dont show.
回答1:
This code is going to work with vector if using
vectorDrawables.useSupportLibrary = true
And change android:src
to app:srcCompat
.
For example,
<ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/triangle"/>
to
<ImageView android:layout_width="match_parent" android:layout_height="match_parent" app:srcCompat="@drawable/triangle"/>
回答2:
I faced a similar problem and @pedja's own answer is useful.
More generally, as mentioned in Chris Banes's article on vector drawable compat, the support library works by injecting its version of ImageView
over the system one on pre-L via some hooks. This implicitly requires the AppCompat versions of classes, such as AppCompatActivity
, be used.
In my case, the vector drawable is used in a standalone toast-like view without an associated activity, using the Application context. I ended up using AppCompatImageView
in the xml layout definition directly, i.e. something like
<android.support.v7.widget.AppCompatImageView
android:id="@+id/some_id"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/selector_referencing_vector_drawable"/>
thus there is no need for the magic "hook" mechanism. As tested this also works with the Activity
class without the need of using AppCompatActivity
. All the above was done without upgrading to 23.2.1, which addressed a different problem.
回答3:
Got this problem too when loading vectors from a selector on pre-lollipop devices:
Use AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) in your onCreate method:
Sets whether vector drawables on older platforms (< API 21) can be used within android.graphics.drawable.DrawableContainer resources. When enabled, AppCompat can intercept some drawable inflation from the framework, which enables implicit inflation of vector drawables within android.graphics.drawable.DrawableContainer resources.
protected final void onCreate(Bundle savedInstanceState) {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
super.onCreate(savedInstanceState);
...
回答4:
The problem was that my activity wasn't extending AppCompatActivity
but regular Activity
.
This is not specified in any documentation/example for support vector drawables
回答5:
The answers given here are ignoring a situation when you wish to add a drawable to a textview because it gives the same error. in my case I had
<TextView .... android:drawableLeft="some_vectore_drawable" />
I could not find how to solve this so I removed that line from the xml code and put it in my java code in this manner
Drawable somevectordrable = AppCompatDrawableManager.get().getDrawable(context, R.drawable.somevectordrawable);
mytextview.setCompoundDrawableWithIntrinsicBounds(somevectordrable, null, null, null);
Clarification for the code,
Get the vector drawable from the drawables folder using AppCompatDrawableManager
Set the drawable we just got as the left drawable on our textview
回答6:
You also need to include the new vector support library:
compile 'com.android.support:support-vector-drawable:23.2.0'
回答7:
It works with 23.2.0 or 23.4.0, not 23.3.0. Seriously Google!
First up, this functionality was originally released in 23.2.0, but then we found some memory usage and Configuration updating issues so we it removed in 23.3.0. In 23.4.0 (technically a fix release) we’ve re-added the same functionality but behind a flag which you need to manually enable.
Ref : https://medium.com/@chrisbanes/appcompat-v23-2-age-of-the-vectors-91cbafa87c88#.waicp19gh
回答8:
That's all I fond over the internet, and it's work for me. Check which one you miss.
1) Inside build.gradle
android {
...
defaultConfig {
...
vectorDrawables.useSupportLibrary = true
}
}
2) Must use buildToolsVersion '27.0.3'
and compile 'com.android.support:appcompat-v7:27.0.3'
similar version code.
3) Use upper then 3 gradle versionclasspath 'com.android.tools.build:gradle:3.0.1'
4) For ImageView
use app:srcCompat
xmlns:app="http://schemas.android.com/apk/res-auto"
app:srcCompat="@drawable/ic_logo"
5) But if you need android:drawableLeft
or android:drawableRigth
etc then:
ic_logo.xml (Vector xml)
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:width="@dimen/home.button.icon"
android:height="@dimen/home.button.icon"
android:viewportWidth="24.0"
android:viewportHeight="24.0"
tools:ignore="VectorRaster">
<path
android:fillColor="#FFFFFF"
android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z"/>
</vector>
ic_logo_select.xml (Vector selector)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@drawable/ic_logo" />
<item android:drawable="@drawable/ic_logo" />
</selector>
Now you can use android:drawableLeft or android:drawableRight
<android.support.v7.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_logo_select"/>
6) And at the end, setCompatVectorFromResourcesEnabled = true inside static which loads before the main method.
public class MainActivity extends AppCompatActivity {
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
}
}
回答9:
If you have this exception issue with drawableLeft
,drawableRight
,drawableTop
, drawableXxx
etc... And your project buildToolsVersion
> 26.0.2
.
Go check your app level gradle file, remove vectorDrawables.useSupportLibrary = true
if exists, then crash gone.
This because buildTools will dispose vector resource after 26.0.2, but somehow conflict with vectorDrawables.useSupportLibrary = true
.
Tested at 2019/03/27.
(if you can't find your buildToolsVersion
in you app level gradle file, this mean you're using Android plugin for Gradle 3.0.0 or higher, your project automatically uses a default version of the build tools that the plugin specifies. In this case your buildToolsVersion
already large than 26.0.2)
回答10:
This is support-v4, appcompat-v7 library v23.2.0 bug it appears in API 19. @tim provide link to this bug issue.
You can upgrade to new library version 23.2.1 and bug is fixed now.
回答11:
If you're having this issue with drawableLeft
/drawableStart
, etc. there is an easy solution, if you're using databinding.
Instead of:
android:drawableLeft="@drawable/somevector" ❌
Do:
android:drawableLeft="@{@drawable/somevector}" ✅
This works because databinding will generate code at compile time that retrieves the drawable in a compatible way.
回答12:
I found this issue on code.google.
It appears to be the same issue as you. Does this help? https://code.google.com/p/android/issues/detail?id=201843
回答13:
If you see yourself using <android.support.v7.widget.AppCompatImageView
when loading a vector drawable it might be a better idea to extend AppCompatActivity
instead of Activity
and go back to using regular <ImageView...app:srcCompat="@drawable/...
来源:https://stackoverflow.com/questions/35819290/invalid-drawable-tag-vector