【问题标题】:Android vector support errorAndroid矢量支持错误
【发布时间】:2017-05-16 07:52:01
【问题描述】:

我的应用在 5.1.0 中运行流畅,但在 4.2.2 中运行时显示错误:

Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector 

我对此进行了研究。它说在gradle中添加向量支持时出错,所以我添加了:

compileSdkVersion 23
buildToolsVersion "25.0.1"
 defaultConfig {
    applicationId "com.wmt.android.troopool"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "0.1"
    multiDexEnabled true
    renderscriptTargetApi 18
    renderscriptSupportModeEnabled true
    vectorDrawables.useSupportLibrary = true
}

compile 'com.android.support:support-vector-drawable:23.2.0'
compile 'com.android.support:animated-vector-drawable:23.2.0'

vectorDrawables.useSupportLibrary = true 和 gradle 文件中的两个依赖项。

但它在 4.4.2 中显示相同的错误。

【问题讨论】:

  • 你是否尝试改变依赖来编译'com.android.support:appcompat-v7:24.2.1'
  • 你必须在你的 ImageViews 上使用app:srcCompat
  • 它在图像视图中工作。但是我如何在文本视图中添加为可绘制的左侧??
  • 矢量资产作为可绘制左侧stackoverflow.com/questions/35761636/…

标签: android vector android-drawable


【解决方案1】:

嘿,要在棒棒糖下面的 android 上使用矢量可绘制对象,您需要从代码中放置图像这里有一些函数可以帮助您做任何您想做的事情

public static enum DrawablePosition {
    Left, Right, Top, Bottom
}

public static Drawable getDrawable(Context context, int drawable) {
    return DrawableCompat.wrap(VectorDrawableCompat.create(context.getResources(), drawable, null));
}

public static Drawable getDrawableWithColor(Context context, int drawableResId, int IntColorOfDrawable) {
    Drawable drawable = VectorDrawableCompat.create(context.getResources(), drawableResId, null);
    DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(drawable, IntColorOfDrawable);
    DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);
    return drawable;
}

public static void setDrawableWithColorForIds(AppCompatActivity activity, int color, int[] ids, int drawable[]) {
    for (int i = 0; i != ids.length; i++) {
        Drawable drawable1 = VectorDrawableCompat.create(activity.getResources(), drawable[i], null);
        DrawableCompat.wrap(drawable1);
        DrawableCompat.setTint(drawable1, color);
        DrawableCompat.setTintMode(drawable1, PorterDuff.Mode.SRC_IN);
        ((TextView) activity.findViewById(ids[i])).setCompoundDrawablesWithIntrinsicBounds(drawable1, null, null, null);
    }
}

public static void placeVectorOnTextView(Context context, TextView textView, int vectorDrawableResID, @Nullable Integer VectorColor, DrawablePosition position) {
    Drawable drawable = VectorDrawableCompat.create(context.getResources(), vectorDrawableResID, null);
    DrawableCompat.wrap(drawable);
    if (VectorColor != null){
        DrawableCompat.setTint(drawable, VectorColor);
        DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);
    }
    switch (position) {
        case Left:
            textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
            break;
        case Bottom:
            textView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawable);
            break;
        case Right:
            textView.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
            break;
        case Top:
            textView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
            break;

    }

}

public static void placeVectorOnEditText(Context context, EditText editText, int vectorDrawableResID, int VectorColor, DrawablePosition position) {
    Drawable drawable = VectorDrawableCompat.create(context.getResources(), vectorDrawableResID, null);
    DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(drawable, VectorColor);
    DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);
    switch (position) {
        case Left:
            editText.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
            break;
        case Bottom:
            editText.setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawable);
            break;
        case Right:
            editText.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
            break;
        case Top:
            editText.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
            break;

    }

}

public static void placeVectorOnButton(Context context, Button button, int vectorDrawableResID, int VectorColor, DrawablePosition position) {
    Drawable drawable = VectorDrawableCompat.create(context.getResources(), vectorDrawableResID, null);
    DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(drawable, VectorColor);
    DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);
    switch (position) {
        case Left:
            button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
            break;
        case Bottom:
            button.setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawable);
            break;
        case Right:
            button.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
            break;
        case Top:
            button.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
            break;

    }

}

public static void placeVectorOnRadioButton(Context context, RadioButton radioButton, int vectorDrawableResID, int VectorColor, DrawablePosition position) {
    Drawable drawable = VectorDrawableCompat.create(context.getResources(), vectorDrawableResID, null);
    DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(drawable, VectorColor);
    DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);
    switch (position) {
        case Left:
            radioButton.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
            break;
        case Bottom:
            radioButton.setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawable);
            break;
        case Right:
            radioButton.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
            break;
        case Top:
            radioButton.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
            break;

    }

}

public static void placeVectorOnCheckBox(Context context, CheckBox checkBox, int vectorDrawableResID, int VectorColor, DrawablePosition position) {
    Drawable drawable = VectorDrawableCompat.create(context.getResources(), vectorDrawableResID, null);
    DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(drawable, VectorColor);
    DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);
    switch (position) {
        case Left:
            checkBox.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
            break;
        case Bottom:
            checkBox.setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawable);
            break;
        case Right:
            checkBox.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
            break;
        case Top:
            checkBox.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
            break;

    }

}

【讨论】:

    猜你喜欢
    • 2016-01-12
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多