Java 以编程方式从 Appcompat 22.2.0 更改浮动操作按钮的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30966222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:26:16  来源:igfitidea点击:

Change color of Floating Action Button from Appcompat 22.2.0 programmatically

javaandroidandroid-layoutfloating-action-button

提问by user2410644

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

我想知道如何更改支持库 22.2.0 中的浮动操作按钮颜色?我试过了

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

提前致谢

回答by florent champigny

the attribute name is backgroundTint

属性名称是 backgroundTint

so I thinks there's a function named

所以我认为有一个名为

button.setBackgroundTint(color)

button.setBackgroundTint(color)

回答by tachyonflux

Create a ColorStateListand set it as the background tint:

创建一个ColorStateList并将其设置为背景色调:

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

回答by Adam Johns

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

colors.xml(R.color.purple在这种情况下)创建一个颜色资源并像这样使用它:

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

回答by Louis CAD

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

在此处检查接受的答案:Android 更改浮动操作按钮颜色

If you wish to change the color

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

如果你想改变颜色

  • 在带有属性app:backgroundTint 的XML 中
  • 在带有.setBackgroundTintList 的代码中

回答by Saeed Darvish

you have to use

你必须使用

  • in XML with attribute app:backgroundTint
  • in code with .setBackgroundTintList read this answer
  • 在 XML 中 attribute app:backgroundTint
  • 在带有 .setBackgroundTintList 的代码中阅读此答案

Android changing Floating Action Button color

Android 更改浮动操作按钮颜色

回答by Maurice

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

如果您使用的是https://github.com/Clans/FloatingActionButton 中的Floating action 按钮库,使用它

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

回答by Olu Smith

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

并从颜色列表中解析实际颜色代码您可以在这里找到

回答by Ralph Pina

To do this backwards compatible:

要做到这一点向后兼容:

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

回答by Afjalur Rahman Rana

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

方法 1:更改 xml 中的浮动操作栏(fab)颜色:

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

要更改浮动操作栏(fab)颜色,只需按照此步骤操作

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

只需在浮动操作栏(fab)的xml中添加“app:backgroundTint =“#colorcode””..例如

app:backgroundTint="#8393ca"

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

在 #8393ca 的位置添加您想要的任何颜色代码

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

方法 2:以编程方式更改浮动操作栏颜色

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

首先在您的 values=>colors 中创建一个红色,然后在创建时将此代码添加到您的活动中

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/

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

回答by Makvin

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

只需在浮动操作按钮下的 xml 文件中使用此行

android:backgroundTint="#96989A"