最近,我想使用Vector Drawable来管理所有图标。 Vector Drawable在Android API 21+上运行良好,但当我尝试在我的一个测试设备[亚马逊KFTHWI Android 4.4.3 API 19]上使用它时,根本无法加载所有图标!
我按照提示Here和Here进行了操作。但它仍然没有用......
以下是我的配置:
在项目gradle脚本文件中:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath ...
}
}
所以我的gradle插件版本应为2.0 +
在主模块gradle脚本文件中:
android{
...
defaultConfig{
...
vectorDrawables.useSupportLibrary = true
...
}
}
dependencies{
...
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:gridlayout-v7:23.4.0'
...
}
此主模块包含该应用程序。 我编译gridlayout-v7库的原因是因为我在应用程序中使用了一些Gridlayouts。
以下是我的实施:
因为我需要根据收到的数据生成一些视图,所以我用java代码设置了图像源。
ImageView icon = new ImageView(this);
icon.setImageResource(R.drawable.ic_lock_black_24dp);
ic_lock_black_24dp.xml是从Vector Asset Studio中的Material Icon库生成的。
我的期望:
我相信我的主模块gradle脚本文件中的配置会阻止Android Studio为Vectors生成Pngs,并允许Android在运行时直接生成Drawables for Vectors。
但我只是部分正确...我的配置实际上阻止了Android Studio在构建时生成Png。但我的旧Android设备[API 19]无法从矢量生成Drawables !!所以现在应用程序中没有图标......
以下是例外情况: [亚马逊KFTHWI Android 4.4.3 API 19]
W/ImageView:Unable to find resource:2130837702
android.content.res.Resources$NotFoundException: File res/drawable/ic_lock_black_24dp.xml from drawable resource ID#0x7f0200c6
at android.content.res.Resources.loadDrawable(Resources.java:2206)
at android.content.res.Resources.getDrawable(Resources.java:707)
at android.widget.ImageView.resolveUri(ImageView.java:651)
at android.widget.ImageView.onMeasure(ImageView.java:774)
at android.view.View.measure(View.java:17038)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
at android.view.View.measure(View.java:17038)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17038)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
at android.view.View.measure(View.java:17038)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
at android.view.View.measure(View.java:17038)
at android.support.v7.widget.GridLayout.measureChildWithMargins2(GridLayout.java:896)
at android.support.v7.widget.GridLayout.measureChildrenWithMargins(GridLayout.java:906)
at android.support.v7.widget.GridLayout.onMeasure(GridLayout.java:945)
at android.view.View.measure(View.java:17038)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17038)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1273)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.widget.ScrollView.onMeasure(ScrollView.java:373)
at android.view.View.measure(View.java:17038)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17038)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:17038)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
at android.view.View.measure(View.java:17038)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5191)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2346)
at android.view.View.measure(View.java:17038)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2004)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1176)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1368)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1057)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5857)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:815)
at android.view.Choreographer.doCallbacks(Choreographer.java:628)
at android.view.Choreographer.doFrame(Choreographer.java:598)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:801)
at android.os.
通过Ironnically,如果我不使用AppCompat支持库,所有图标都会很好用,但我知道这是因为Android Studio在构建时会生成Png。我甚至可以在APK中找到那些Png。
我的问题:
现在,我仍然希望从旧的Android设备中的Vector Drawable中受益,这意味着我仍然试图避免使用Png ...有人可以给我一些提示或者告诉我在哪些步骤中我做错了什么?
答案 0 :(得分:4)
你必须说new AppCompatImageView(this)
。手动创建视图时,您必须添加AppCompat
前缀。
答案 1 :(得分:1)
有一个库支持api 14+ for VectorDrawable和AnimatedVectorDrawable我使用:https://github.com/wnafee/vector-compat