2017-01-02 69 views
3

我的應用程序運行順暢的5.1.0,但是當我在4.2.2運行它顯示了一個錯誤:Android的矢量支持錯誤

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中顯示了相同的錯誤。

+0

做你試圖改變依賴編譯「什麼com.android.support:appcompat-v7:24.2.1' – siddhesh

+0

你必須在你的ImageViews –

+0

上使用'app:srcCompat'在圖像視圖中工作。但我如何添加文本視圖爲可繪製的左? –

回答

0

嘿在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; 

    } 

}