Bir FloatingActionButton'unda bir simge kaynak resmi nasıl tonlanır? favoriteFab.setColorFilter(R.color.yellow, PorterDuff.Mode.OVERLAY);
'u denedim, ancak başarı yok.FloatingActionButton'da Android renk tonu simgesi
cevap
API 21 veya üstünü kullanıyorsanız, bu şekilde çizilebilir renk tonunu ayarlayabilirsiniz.
mFAB.getDrawable() mutate() setTint (getResources(). GetColor (R.color.yourColor));
E.g.
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));
Güncelleme: GetColor yana yerine ContextCompat kullanmalısınız kullanımdan kaldırıldı. Aşağıdaki örneğin kullanın:
mFAB.getDrawable().mutate().setTint(ContextCompat.getColor(this, R.color.colorAccent));
Teşekkürler Vieuser, ben sadece – fab
çalışır API sadece çalışır> 21 –
Sadece API> 21 üzerinde çalışır. Daha düşük API'larda kullanmak istiyorsanız cevabımı kontrol edin –
Drawable fabDr= mFAB.getDrawable();
DrawableCompat.setTint(fabDr, Color.WHITE);
yapabilirsiniz basit kullanım desteği-v4 içinde DrawableCompat şöyle:
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);
I varsayarak favoriteFab sizin FloatingActionButton olduğunu. Sen kullanabilirsiniz:
int color = ContextCompat.getColor(this, R.color.yellow);
favoriteFab.getDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
Bunun yerine DrawableCompat.setTintList()
kullanabilirsiniz:
Drawable drawable = DrawableCompat.wrap(fab.getDrawable());
DrawableCompat.setTint(drawable, myColorInt);
fab.setImageDrawable(drawable);
Ben -5 alıyorum Neden, bu sadece bir sorudur ?? – fab
Ben kendimi merak ediyordum .. belki detay eksikliği? Soruyu anlayabiliyorum .. idk. –