Change color of Floating Action Button from Appcom

2019-02-05 17:49发布

问题:

I would like to know how to change the Floating Action Button color from the Support library 22.2.0 ? I've tried

button.setBackgroundColor(color);

but clearly, this changes the drawable of the button and it turns to a square.

Now I wonder how to change the color but just the color, without touching the shape?

Thanks in advance

回答1:

Maybe late but could help.

 fab.setBackgroundTintList(ColorStateList.valueOf(Color
                    .parseColor("#33691E")));

and parse the actual color code from a list of colors You can find here



回答2:

Create a ColorStateList and set it as the background tint:

button.setBackgroundTintList(new ColorStateList(new int[][]{new int[]{0}}, new int[]{color}));


回答3:

you have to use

  • in XML with attribute app:backgroundTint
  • in code with .setBackgroundTintList read this answer

Android changing Floating Action Button color



回答4:

Create a color resource in colors.xml (R.color.purple in this case) and use it like so:

floatingActionButton.setBackgroundTintList(getResources().getColorStateList(R.color.purple));


回答5:

Method 1: Change floating action bar(fab) color in xml:

To change floating action bar(fab) color just follow this step

just add "app:backgroundTint="#colorcode" " in xml of floating action bar(fab) .. For example

app:backgroundTint="#8393ca"

at the place of #8393ca add any color code you want

Example as usaage..

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:backgroundTint="#8393ca"
    android:src="@drawable/send" />

Method 2: Change floating action bar color programmatically

just add this line on your code

Firstly create a color red in your values=>colors then add this code in your activity on create

fab.setBackgroundTintList(getResources().getColorStateList(R.color.red));

                                or

fab.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#33691E")));

credits : http://androidrace.com/2016/12/12/how-to-change-fabfloating-action-bar-color-android/



回答6:

Check the accepted answer here: Android changing Floating Action Button color

If you wish to change the color

  • in XML with attribute app:backgroundTint
  • in code with .setBackgroundTintList


回答7:

To do this backwards compatible:

DrawableCompat.setTintList(DrawableCompat.wrap(fab.getDrawable()), tintColor); // <- icon
DrawableCompat.setTintList(DrawableCompat.wrap(fab.getBackground()), backgroundTintColor); // <- background


回答8:

the attribute name is backgroundTint

so I thinks there's a function named

button.setBackgroundTint(color)



回答9:

if you are using Floating action button library from https://github.com/Clans/FloatingActionButton then use this

fab.setColorNormal(getResources().getColor(R.color.fab_color1));


回答10:

just use this line in your xml file under floating action button

android:backgroundTint="#96989A"


回答11:

Try this code. It will add a tint to the background resource.

button.setBackgroundTintList(getResources().getColorStateList(R.color.yourColor));


回答12:

If you are use Xamarin for android app then try below code

FloatingActionButton fa = root.FindViewById<FloatingActionButton>(Resource.Id.fabback);
            Android.Content.Res.ColorStateList csl = new Android.Content.Res.ColorStateList(new int[][] { new int[0] }, new int[]{Android.Graphics.Color.ParseColor("#000000") });
            fab.BackgroundTintList= csl;

"#000000" used for black color you can changed according to your requriments.