在我的应用程序中,我使用在支持库23.2中添加的矢量drawables用于显示矢量图标,它工作得很好但是当我将矢量设置为EditText的drawableLeft时,它在pre-lollipop android版本中不起作用.在运行时,发生ResourceNotFound异常.
Caused by: android.content.res.Resources$NotFoundException: File
res/drawable/layer_ic_user.xml from drawable resource ID #0x7f0200b3
Run Code Online (Sandbox Code Playgroud)
这是我的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'
Run Code Online (Sandbox Code Playgroud)
EditText:
<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"
/>
Run Code Online (Sandbox Code Playgroud)
您可以以编程方式在 EditText 中添加 Vector Drawable。利用VectorDrawableCompat添加 drawableLeft/drawableRight/drawableTop/drawableBottom/drawableStart/drawableEnd。
脚步:
一世。消除android:drawableLeft="@drawable/layer_list_ic_user"
ii. 如果 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);
Run Code Online (Sandbox Code Playgroud)
三、如果 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);
Run Code Online (Sandbox Code Playgroud)
有关 VectorDrawableCompat 的更多信息,请参阅此 链接
更新
\n\n自 Android 支持库修订版 23.4.0 起
\n\n\n\n\n添加了 AppCompatDelegate.setCompatVectorFromResourcesEnabled() 方法,以在运行 Android 4.4(API 级别 19)及更低版本的设备上重新启用在 DrawableContainer 对象中使用矢量可绘制对象。有关详细信息,请参阅向量的 AppCompat v23.2\xe2\x80\x8a\xe2\x80\x94\xe2\x80\x8aAge 。
\n
您应该将其添加static {\n AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);\n}
到“活动”的顶部。
您正在使用 AppCompat 23.3。来自Android 开发者
\n\n对于 AppCompat 用户,由于版本 23.2.0/23.2.1 中的实现中发现问题,我们\xe2\x80\x99 决定删除允许您在 Lollipop 之前的设备上使用资源中的矢量绘图的功能。使用 app:srcCompat 和 setImageResource() 继续有效。
\n
归档时间: |
|
查看次数: |
2328 次 |
最近记录: |