Wie tönt ein Symbolressourcenbild in einem FloatingActionButton? Ich habe versucht favoriteFab.setColorFilter(R.color.yellow, PorterDuff.Mode.OVERLAY);
aber keinen Erfolg.Android-Tönungssymbol in FloatingActionButton
Antwort
Sie können den Farbton des Zeichensatzes wie folgt festlegen, wenn Sie API 21 oder höher verwenden.
mFAB.getDrawable(). Mutate(). SetTint (getResources(). GetColor (R.color.yourColor));
z.
mFAB = (FloatingActionButton) findViewById(R.id.fab);
mFAB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v, "Yummy snackbar", LENGHT_LONG).show();
}
});
mFAB.getDrawable().mutate().setTint(getResources().getColor(R.color.colorAccent));
Update: Seit getColor veraltet ist Sie ContextCompat stattdessen verwenden sollten. Verwenden Sie die folgende z:
mFAB.getDrawable().mutate().setTint(ContextCompat.getColor(this, R.color.colorAccent));
danke Vieuser, ich werde versuchen – fab
funktioniert nur API> 21 –
Es funktioniert nur auf API> 21. Überprüfen Sie meine Antwort, wenn Sie es auf niedrigeren APIs verwenden möchten –
Drawable fabDr= mFAB.getDrawable();
DrawableCompat.setTint(fabDr, Color.WHITE);
Sie können einfache Nutzung der DrawableCompat in support-v4 wie folgt:
Drawable drawable = mFloatingActionButton.getDrawable();
// Wrap the drawable so that future tinting calls work
// on pre-v21 devices. Always use the returned drawable.
drawable = DrawableCompat.wrap(drawable);
// We can now set a tint
DrawableCompat.setTint(drawable, ContextCompat.getColor(this, R.color.white));
// ...or a tint list
DrawableCompat.setTintList(drawable, ColorStateList.valueOf(ContextCompat.getColor(this, R.color.white)));
// ...and a different tint mode
DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_OVER);
Ich gehe davon aus favoriteFab ist Ihr FloatingActionButton. Sie können:
int color = ContextCompat.getColor(this, R.color.yellow);
favoriteFab.getDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
Sie DrawableCompat.setTintList()
stattdessen verwenden können:
Drawable drawable = DrawableCompat.wrap(fab.getDrawable());
DrawableCompat.setTint(drawable, myColorInt);
fab.setImageDrawable(drawable);
Warum ich -5 bekommen, das ist nur eine Frage ?? – fab
Ich habe mich selbst gefragt .. vielleicht Mangel an Details? Ich kann die Frage jedoch verstehen .. idk. –