在我的应用程序中,我使用 support library 23.2 中添加的 vector drawables 来显示矢量图标,它工作得很好,但是当我将矢量设置为 EditText 的 drawableLeft 时,它确实如此不适用于 Lollipop 之前的安卓版本。 在运行时,发生ResourceNotFound 异常。
Caused by: android.content.res.Resources$NotFoundException: File
res/drawable/layer_ic_user.xml from drawable resource ID #0x7f0200b3
这是我的 gradle 配置:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.test"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary = true
generatedDensities = []
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
sourceSets { main { assets.srcDirs = ['src/main/assets', 'src/main/res/assets/'] } }
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
compile 'com.android.support:design:23.3.0'
}
apply plugin: 'com.google.gms.google-services'
编辑文本:
<EditText
android:id="@+id/et_username_or_email"
android:layout_width="@dimen/edit_text_width"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/layer_list_ic_user"
android:textColorHint="@color/ColorBlackPrimary"
android:inputType="textEmailAddress|text"
android:textColor="@color/ColorBlackPrimary"
android:textSize="@dimen/text_small"
/>
最佳答案
您可以通过编程方式在 EditText 中添加 Vector Drawable。使用 VectorDrawableCompat 来添加 drawableLeft/drawableRight/drawableTop/drawableBottom/drawableStart/drawableEnd。
步骤:
我。删除 android:drawableLeft="@drawable/layer_list_ic_user"
二。如果 EditText 在 Activity 中:
EditText etUserName= (EditText)findViewById(R.id.et_username_or_email);
VectorDrawableCompat drawableCompat=VectorDrawableCompat.create(getResources(), R.drawable.layer_list_ic_user, etUserName.getContext().getTheme());
etUserName.setCompoundDrawablesRelativeWithIntrinsicBounds(drawableCompat, null, null, null);
三。如果 EditText 在 Fragment 中:
EditText etUserName= (EditText)view.findViewById(R.id.et_username_or_email);
VectorDrawableCompat drawableCompat=VectorDrawableCompat.create(getActivity().getResources(), R.drawable.layer_list_ic_user, etUserName.getContext().getTheme());
etUserName.setCompoundDrawablesRelativeWithIntrinsicBounds(drawableCompat, null, null, null);
有关 VectorDrawableCompat 的更多信息,请参阅此 link
关于android - EditText drwableLeft 不适用于向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36661068/