Change color of Floating Action Button from Appcom

2019-02-05 17:19发布

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

12条回答
仙女界的扛把子
2楼-- · 2019-02-05 17:50

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

button.setBackgroundTintList(getResources().getColorStateList(R.color.yourColor));
查看更多
时光不老,我们不散
3楼-- · 2019-02-05 17:55

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.

查看更多
干净又极端
4楼-- · 2019-02-05 17:56

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));
查看更多
地球回转人心会变
5楼-- · 2019-02-05 18:03

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

android:backgroundTint="#96989A"
查看更多
倾城 Initia
6楼-- · 2019-02-05 18:04

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/

查看更多
贼婆χ
7楼-- · 2019-02-05 18:04

To do this backwards compatible:

DrawableCompat.setTintList(DrawableCompat.wrap(fab.getDrawable()), tintColor); // <- icon
DrawableCompat.setTintList(DrawableCompat.wrap(fab.getBackground()), backgroundTintColor); // <- background
查看更多
登录 后发表回答