我使用的是Design Support Library版本23.4.0。我启用了gradle标志:
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
我正在使用构建工具版本23.0.2,但仍然,我在KitKat或更低版本上获得Resources$NotFoundException
。
它发生在我使用android:drawableLeft
或imageView.setImageResource(R.drawable.drawable_image)
时。
是的,我把它放在我使用drawables的每个活动上
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
这是支持库的错误吗?
我需要使用支持库23.4.0才能使用3个单独的东西:
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
android:src
同
app:srcCompat
并在代码中替换为:
imageView.setImageResource(...);
同
imageView.setImageDrawable(...);
此支持库中的`VectorDrawable`
和`AnimatedVectorDrawable`
可以通过以下方式膨胀:
getDrawable()
方法://This will only inflate a drawable with <vector> as the root element VectorDrawable.getDrawable(context, R.drawable.ic_arrow_vector); //This will only inflate a drawable with <animated-vector> as the root element AnimatedVectorDrawable.getDrawable(context, R.drawable.ic_arrow_to_menu_animated_vector); // This will inflate any drawable and will auto-fallback to the lollipop implementation on api 21+ devices ResourcesCompat.getDrawable(context, R.drawable.any_drawable);
如果在java代码中膨胀Drawable,建议始终使用ResourcesCompat.getDrawable()
,因为这适用于处理Lollipop后备。这允许系统缓存Drawable ConstantState,因此更有效。
该库具有以下变形(双向)动画:
API 16
phone:import com.wnafee.vector.compat.AnimatedVectorDrawable;
mdrawable = (AnimatedVectorDrawable) AnimatedVectorDrawable.getDrawable(this.getApplicationContext(), R.drawable.consolidated_animated_vector);
在这里查看vector-compat
的github自述文件:https://github.com/wnafee/vector-compat
如果你将它与你的app模块的API 14
build.gradle
(通常在文件的末尾)合并,这将解决你的问题(下至dependencies
):
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//Trying to FIX Binary XML file line #2: invalid drawable tag animated-vector
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
//not needed
// compile 'com.android.support:support-vector-drawable:25.0.0'
compile 'com.wnafee:vector-compat:1.0.5'//*******holy grail *******https://github.com/wnafee/vector-compat
// Failed to resolve: com.android.support:support-animated-vector-drawable:25.0.0
//not needed
// compile 'com.android.support:support-animated-vector-drawable:25.0.0'
}
不要把你的载体放在drawable-anydpi
,旧设备不支持
把它们放在drawable
在我的特定情况下,我遇到了这个问题,因为我使用了一个drawable选择器作为图像资源,在选择器中有几个向量,如:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/vector_select_blue"/>
<item android:state_pressed="true" android:drawable="@drawable/vector_select_black"/>
.
.
etc
</selector>
是的,非常糟糕,但当时并不知道更好。
因此,正确的方法是在矢量文件中使用tint属性,如:
<vector ..vector properties..
android:tint="@color/vector_color_selector">
<path ..path properties../>
</vector>
(您还可以在AppCompatImageView中使用app:tint属性)
现在,您的vector_color_selector文件应该具有您想要的颜色,如:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/blue"/>
<item android:state_pressed="true" android:color="@color/black"/>
.
.
etc
</selector>
如果以前的答案不适合你,我希望这可以帮助别人。说明显而易见,但我必须说你还需要在gradle中设置vectorDrawables.useSupportLibrary = true,使用AppCompatImageView并使用app:srcCompat或setImageDrawable + AppCompatResources.getDrawable来避免使用vector compat库的任何麻烦。
正如Harish Gyanani在评论中所说的那样使用AppCompatImageView
而不是ImageView
,它适用于我。
为了补充这里的一些答案:对VectorDrawables的向后兼容支持需要付出代价,并不适用于所有情况。
它在哪些情况下有效?我已经让this diagram提供帮助(对支持库23.4.0有效至少25.1.0)。
尝试使用:
imageView.setImageDrawable(VectorDrawableCompat.create(getResources(), drawableRes, null));
您不必以这种方式添加AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
。
只需使用VectorDrawableCompat对您的矢量绘图进行充气,就可以了。
我们遇到了同样的问题。在Kitkat上看不到矢量drawables。我通过将AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
添加到活动的onCreate方法来解决了这个问题。
在此之前不要忘记添加:
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
并为您使用矢量drawable的视图调用setImageResource。我的观点是ImageButton。我有Android SDK构建工具版本23.0.3
很抱歉迟到了,但这个答案可能会帮助那些想要启用标志的用户AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);所有活动。
1.创建一个扩展到Application(android.app.Application)的类
public class MyApplicationClass extends Application
{
@Override
public void onCreate()
{
super.onCreate();
}
}
2.转到Manifest.xml并将以下行添加到标记中
<application
android:name=".MyApplicationClass"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
...
</application>
3.在MyApplicationClass.java中的onCreate上添加以下代码
// This flag should be set to true to enable VectorDrawable support for API < 21
static
{
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
MyApplicationClass.java的完整代码
import android.app.Application;
import android.support.v7.app.AppCompatDelegate;
/**
* Created by Gaurav Lonkar on 23-Dec-17.
*/
public class MyApplicationClass extends Application
{
// This flag should be set to true to enable VectorDrawable support for API < 21
static
{
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
@Override
public void onCreate()
{
super.onCreate();
}
}
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
在app.gradle中使用它
然后使用AppCompatDrawableManager
来设置setDrawable和getDrawable。适合我
在android:drawableLeft
这样的地方支持矢量绘图在支持库23.3中被禁用。它在Google+上宣布:
由于23.2.0 / 23.2.1版本中的实现中发现的问题,我们决定删除允许您在Lollipop前设备上使用矢量drawable的功能。使用app:srcCompat和setImageResource()继续工作。
问题链接:
但是,如果你能解决这些问题,可以在23.4中使用AppCompatDelegate.setCompatVectorFromResourcesEnabled()重新启用此功能。
如果你对这是如何工作感到好奇,那么最好的人就是Chris Banes,他是这个功能的创作者。他详细解释了on his blog。
更改
imageView.setImageResource(R.drawable.drawable_image)
至
imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.drawable_image));
如果你想在xml中使用vectordrawable,请使用:
app:srcCompat="@drawable/drawable_image"
我很久以前遇到过类似的问题,设置不起作用
vectorDrawables.useSupportLibrary = true
仅在我创建“mipmap”文件夹和使用的代码时才有效
imageView.setImageResource (R.mipmap.drawable_image)
它有更多的信息here