EditText中的drawableLeft不能与矢量图一起使用

12

在我的应用程序中,我使用在support库23.2中添加的矢量可绘制对象来显示矢量图标,它运作得非常完美。但是,当我将矢量图标设置为EditText的drawableLeft时,在早期的Android版本中不起作用。在运行时,会出现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"
        />

2
使用 TextView#setCompoundDrawables - pskink
https://dev59.com/H-k6XIcBkEYKwwoYC_n6 - Chaudhary Amar
@pskink,我该如何使用TextView#setCompoundDrawables方法? - Edalat Feizi
如何使用?只需使用3个空Drawable和一个非空Drawable调用它即可。 - pskink
6个回答

6

您可以在代码中添加可缩放矢量图形(Vector Drawable)到EditText。使用VectorDrawableCompat将drawableLeft / drawableRight / drawableTop / drawableBottom / drawableStart / drawableEnd添加到其中。

步骤:

i. 移除 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);

iii. 如果 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的更多信息,请参阅此链接


1
尝试了所有其他解决方案,这是一个简单而干净的解决方案,实际上起作用了。 - BarLr

3

更新

自 Android Support Library 版本为 23.4.0 以来,新增了 AppCompatDelegate.setCompatVectorFromResourcesEnabled() 方法,可以在运行 Android 4.4(API 级别 19)及以下版本的设备上重新启用对 DrawableContainer 对象中矢量图绘制的支持。有关详细信息,请参见AppCompat v23.2 – Age of the vectors

您应该将以下代码添加到 Activity 的顶部:

static { AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); }


您正在使用 AppCompat 23.3 版本。摘自Android Developers

对于 AppCompat 用户,我们决定删除在版本 23.2.0/23.2.1 中实现时发现的问题的功能,该功能允许您从资源中使用矢量图形在 Lollipop 之前的设备上使用。但是使用 app:srcCompat 和 setImageResource() 仍可正常工作。


1
app:srcCompat在ImageView上运行良好,我想在EditText的drawableLeft中使用矢量图。 - Edalat Feizi
1
你可以通过编程来实现:VectorDrawableCompat.create() - John
1
除了 setCompatVectorFromResourcesEnabled 之外,我还需要将矢量图包装在选择器中,才能在 drawableRight 中使用它。正如 矢量图时代 中所提到的:唯一的规则是矢量图需要在单独的文件中 - arekolek

2
我遇到了这个问题,并通过将向量图像放入层列表可绘制中来解决它,如下所示:search_grey.xml
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_search_grey" /> 
</layer-list>

在EditText中:

        android:drawableLeft="@drawable/search_grey"

2
AppCompatTextView现在支持app:drawableLeftCompatapp:drawableTopCompatapp:drawableRightCompatapp:drawableBottomCompatapp:drawableStartCompatapp:drawableEndCompat复合可绘制图像,支持后向兼容的可绘制类型,例如VectorDrawableCompat

请在您的gradle文件中包含此内容。

implementation 'androidx.appcompat:appcompat:1.1.0'

在你的TextView/EditText中,你可以使用:
app:drawableLeftCompat
app:drawableStartCompat

1

矢量可绘制对象在早期的 Android 设备上无法像 drawableLeft/drawableRight/.. 一样工作,不要在 EditText 的 xml 中设置 drawableLeft。在初始化 EditText 后,可以按以下方式设置 drawableLeft。

et_username_or_Email.setCompoundDrawablesWithIntrinsicBounds(R.drawable.layer_list_ic_user, 0, 0, 0);

0
在我的情况下,只需要在Activity类文件中将extends Activity更改为extends AppCompatActivity即可解决问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接