2016-04-20 8 views
12

ich die DrawableCompat bin mit ziehbar, wie unten für Tönung, Tönungs scheint nicht am 19. API zu funktionieren Ich verwende die Unterstützung lib Version 23.3.0DrawableCompat setTint funktioniert nicht auf API 19

Drawable drawable = textView.getCompoundDrawables()[drawablePosition]; 
if (drawable != null) { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      drawable.setTint(color); 
     } else { 
      DrawableCompat.setTint(DrawableCompat.wrap(drawable), color); 
     } 
    } 

Antwort

19

Ich hatte das gleiche Problem. Ich kombinierte die Beiträge in https://stackoverflow.com/a/30928051 und versuchte die APIs 17, 19, 21, 22, 23 und N Preview 3 mit SupportLib 23.4.0, um eine Lösung zu finden.

Auch wenn erwähnt wird, dass die compat-Klasse einen Filter für Pre-Lollipop-Geräte verwendet (siehe https://stackoverflow.com/a/27812472/2170109), funktioniert es nicht.

Jetzt überprüfe ich die API selbst und benutze den folgenden Code, der an allen getesteten APIs funktioniert (für 17 und höher).

// https://stackoverflow.com/a/30928051/2170109 
    Drawable drawable = DrawableCompat.wrap(ContextCompat.getDrawable(context, R.drawable.vector)); 
    image.setImageDrawable(drawable); 

    /* 
    * need to use the filter | https://stackoverflow.com/a/30880522/2170109 
    * (even if compat should use it for pre-API21-devices | https://stackoverflow.com/a/27812472/2170109) 
    */ 
    int color = ContextCompat.getColor(context, R.color.yourcolor); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
     DrawableCompat.setTint(drawable, color); 

    } else { 
     drawable.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN); 
    } 
+1

schließlich etwas, das für mich auf Lutscher gearbeitet . Vielen Dank für das Teilen. – Alin

+1

Es funktioniert nicht für mich auf Lollipop (API 21). Ich musste die minimale Version in API 22 (Build.VERSION_CODES.LOLLIPOP_MR1) ändern. – Eselfar

+0

Die Kombination der Antworten von Hardysim und @Eselar arbeitete in meinem Fall. – LambergaR

7

Arbeiten auf API 15-25 mit AppCompat Support Library (getestet auf 24.1.1 und höher).

public static Drawable getTintedDrawable(@NonNull final Context context, 
             @DrawableRes int drawableRes, @ColorRes int colorRes) { 
    Drawable d = ContextCompat.getDrawable(context, drawableRes); 
    d = DrawableCompat.wrap(d); 
    DrawableCompat.setTint(d.mutate(), ContextCompat.getColor(context, colorRes)); 
    return d; 
} 
+0

Ich versuchte mit der Support-Bibliothek 24.2.1 und es funktioniert leider nicht auf API 19. Ich ging zu @ hardysims Lösung über. – Eselfar

+0

es funktioniert für mich, ich müde auf API 19 – JFouad

+0

Arbeitet für mich auf API 19/21/25 mit Support-Bibliothek 25.1.1 – mVck

0

basierend auf @localhost Antwort

Drawable d = DrawableCompat.wrap(ContextCompat.getDrawable(this, R.drawable.ic_rate)); DrawableCompat.setTint(d, Color.parseColor("#AAAAAA")); l.setLogo(d);

ich auf API versucht, 19> 25 und es funktioniert gut