android - EditText drwableLeft 不适用于向量

标签 android vector

在我的应用程序中,我使用 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/

相关文章:

android - 无法在 flutter 应用程序中获取用户位置

r - 使用向量将多个 csv 文件读入到 r

带有函数指针的 C++ vector push_back

c++ - MPI 在 C++ 中发送具有 vector 属性的结构

c++ - std::vector 和 std::list 的重载运算符

r - 具体例子说明为什么R中没有为向量定义维度(向量是无量纲的)?

android - 如何以编程方式在android中创建视频

android - 想要从特定文件夹中选择图像文件,但拥有图库的所有文件

android - 我可以通过 Firebase 禁用推送通知吗?

android - FrameLayout 高度与父项不匹配