Im essayant de changer la couleur d'un dessin TextView dans Xamarin.
Dans Java vous pouvez le faire comme ceci:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView txt = (TextView) findViewById(R.id.my_textview);
setTextViewDrawableColor(txt, R.color.my_color);
}
private void setTextViewDrawableColor(TextView textView, int color) {
for (Drawable drawable : textView.getCompoundDrawables()) {
if (drawable != null) {
drawable.setColorFilter(new PorterDuffColorFilter(getColor(color), PorterDuff.Mode.SRC_IN));
}
}
}
Comment puis-je faire quelque chose comme ça dans Xamarin.Android?
Essayez la solution ci-dessous
private void setTextViewDrawableColor(TextView textView, int color) {
for (Drawable drawable : textView.getCompoundDrawables()) {
if (drawable != null) {
drawable.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(textView.getContext(), color), PorterDuff.Mode.SRC_IN));
}
}
}
J'utilise ceci dans kotlin:
tv.getCompoundDrawables()[0].setTint(//color)
Je résous ce problème en ajoutant dans la définition xml cette ligne:
Android: drawableTint = "@ color/red"
Un exemple complet:
<TextView Android:id="@+id/tv_element" Android:layout_width="wrap_content" Android:layout_height="20dp" Android:layout_alignParentEnd="true" Android:drawableStart="@drawable/ic_icon" Android:drawableTint="@color/color" Android:visibility="visible" />