【问题标题】:EditText drwableLeft don't work with vectorsEditText drwableLeft 不适用于矢量
【发布时间】:2016-04-16 06:46:15
【问题描述】:

在我的应用程序中,我使用 support library 23.2 中添加的 vector drawables 来显示矢量图标,它可以完美运行,但是当我将矢量设置为 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"
        />

【问题讨论】:

  • 使用TextView#setCompoundDrawables
  • @pskink 我如何使用 TextView#setCompoundDrawables
  • 怎么用?只需用 3 个 null Drawable 和一个非 null Drawable 调用它

标签: android vector


【解决方案1】:

您可以通过编程方式在 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

【讨论】:

  • 在这里尝试了所有其他解决方案,这是一个真正有效的简单而干净的解决方案。
【解决方案2】:

更新

自 Android 支持库,修订版 23.4.0

添加了 AppCompatDelegate.setCompatVectorFromResourcesEnabled() 方法,以在运行 Android 4.4(API 级别 19)及更低版本的设备上重新启用 DrawableContainer 对象中的矢量可绘制对象。请参阅AppCompat v23.2 — Age of the vectors 了解更多信息。

您应该将static { AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); } 添加到您的活动顶部。


您正在使用 AppCompat 23.3。来自Android Developers

对于 AppCompat 用户,由于版本 23.2.0/23.2.1 中的实施存在问题,我们决定删除允许您从棒棒糖之前的设备资源中使用矢量可绘制对象的功能。使用 app:srcCompat 和 setImageResource() 继续有效。

【讨论】:

  • app:srcCompat 适用于图像视图我想将矢量用于 EditText 的 drawableLeft
  • 你可以通过编程实现:VectorDrawableCompat.create()
  • 除了setCompatVectorFromResourcesEnabled,我还必须将可绘制的矢量包装在选择器中,以便与drawableRight 一起使用。正如Age of the vectors 中提到的:唯一的规则是向量需要在单独的文件中
【解决方案3】:

我遇到了这个问题,并通过将矢量图像放入 layer-list drawable 中来解决它: 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"

【讨论】:

    【解决方案4】:

    矢量drawables 不能在棒棒糖之前的设备上作为drawableLeft/drawableRight/..工作,不要在EditText xml 上设置drawableLeft。 初始化 EditText 后,设置 drawableLeft 如下所示。

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

    【讨论】:

      【解决方案5】:

      AppCompatTextView now 支持 app:drawableLeftCompatapp:drawableTopCompatapp:drawableRightCompatapp:drawableBottomCompatapp:drawableStartCompatapp:drawableEndCompat 复合可绘制对象,支持向后移植的可绘制类型,例如 VectorDrawableCompat .

      将其包含在您的 gradle 文件中

      implementation 'androidx.appcompat:appcompat:1.1.0'
      

      在你的 TextView/EditText 中你可以使用

      app:drawableLeftCompat
      app:drawableStartCompat
      

      【讨论】:

        【解决方案6】:

        就我而言,

        将Activity类文件中的extends Activity改成extends AppCompatActivity,问题就解决了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-31
          • 1970-01-01
          • 1970-01-01
          • 2019-11-19
          • 1970-01-01
          • 1970-01-01
          • 2011-10-18
          • 1970-01-01
          相关资源
          最近更新 更多