我的应用程序使用矢量可绘制对象,并且在API级别24以下的所有设备上都能正常工作。我已经在gradle文件中启用了android.defaultConfig.vectorDrawables.useSupportLibrary = true
,下面是imageView的代码
<android.support.v7.widget.AppCompatImageView
android:layout_marginTop="3dp"
android:id="@+id/imageView1"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:scaleType="fitCenter"
app:srcCompat="@mipmap/ic_launcher">
</android.support.v7.widget.AppCompatImageView>
我以编程方式像view.imgView.setImageResource( iconImage.get( position ) );
那样动态地更改图像,imageImage是一个ArrayList,其中包含不同的图像,并使用此代码填充
private void prepareList() {
iconImage = new ArrayList<>();
iconText = new ArrayList<>();
iconImage.add(R.drawable.ic_new_profile);
iconText.add( LanguageSwitch.myProfile );
iconImage.add( R.drawable.ic_new_contributions );
iconText.add( LanguageSwitch.contributions );
iconImage.add( R.drawable.ic_new_benefits );
iconText.add( LanguageSwitch.benefits );
iconImage.add( R.drawable.ic_new_claims );
iconText.add( LanguageSwitch.claims );
iconImage.add( R.drawable.ic_new_dependents );
iconText.add( LanguageSwitch.dependants );
iconImage.add(R.drawable.ic_new_cards );
iconText.add( LanguageSwitch.mycard );
iconImage.add(R.drawable.ic_new_facility );
iconText.add( LanguageSwitch.facilities );
iconImage.add( R.drawable.ic_new_preauth );
iconText.add( getResources().getString( R.string.preuath ) );
iconImage.add(R.drawable.ic_new_contact_us );
iconText.add( LanguageSwitch.contact_us );
}
我的gradle文件看起来像这样:
android {
compileSdkVersion 26
buildToolsVersion '27.0.3'
defaultConfig {
applicationId "xxx.xxx.xxxx"
minSdkVersion 18
targetSdkVersion 26
versionCode 32
versionName "2.0.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
android.defaultConfig.vectorDrawables.useSupportLibrary = true
applicationId "com.ngenx.nhif"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE-FIREBASE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/rxjava.properties'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
最后是日志文件:
Process: com.ngenx.nhif, PID: 5233
android.content.res.Resources$NotFoundException: Drawable com.ngenx.nhif:drawable/ic_new_profile with resource ID #0x7f0800d3
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_new_profile.xml from drawable resource ID #0x7f0800d3
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:725)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571)
at android.content.res.Resources.getDrawable(Resources.java:767)
at android.content.Context.getDrawable(Context.java:525)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:358)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:85)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:93)
at com.ngenx.nhif.MainMenuAdapter.getView(MainMenuAdapter.java:76)
at android.widget.AbsListView.obtainView(AbsListView.java:2362)
at android.widget.GridView.onMeasure(GridView.java:1065)
at android.view.View.measure(View.java:19734)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1619)
at android.view.View.measure(View.java:19734)
答案 0 :(得分:0)
defaultConfig { vectorDrawables.useSupportLibrary = true }
工作正常,我遇到的问题是一些损坏的svg图标。在将它们替换为可以正常工作的旧版本后,我意识到了这一点。