本文共 4077 字,大约阅读时间需要 13 分钟。
本文翻译自:
Been trying for hours to change Material's Floating Action Button color, but without success. 一直试图改变材质的浮动动作按钮颜色,但没有成功。
I have tried to add 我试过补充一下
android:background="@color/mycolor"
or via code 或通过代码
FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.profile_edit_fab);fab.setBackgroundColor(Color.parseColor("#mycolor"));
or 要么
fab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#mycolor")));
But none of the above worked. 但以上都没有奏效。 I have also tried the solutions in the "duplicate question" , but none of them works, button remained green and also became a square. 我也在“重复的问题”中尝试了解决方案,但它们都没有工作,按钮保持绿色并且也变成了正方形。
I'd like a detailed answer, thanks. 我想详细解答一下,谢谢。
PS It would be also nice to know how to add ripple effect, couldn't understand that either. PS知道如何添加涟漪效果也不错,也无法理解。
参考:
The FAB is colored based on your colorAccent
. FAB根据您的colorAccent
着色。
The document suggests that it takes the @color/accent by default. 该文档表明它默认采用@ color / accent。 But we can override it on code by using 但是我们可以通过使用来覆盖代码
fab.setBackgroundTintList(ColorStateList)
Also remember, 还记得,
The minimum API version to use this library is 15 so you need to update it! 使用此库的最低API版本为15,因此您需要更新它! if you dont want to do it then you need to define a custom drawable and decorate it! 如果你不想这样做,那么你需要定义一个自定义drawable并装饰它!
As described in the , by default it takes the color set in styles.xml attribute colorAccent . 如中所述,默认情况下,它采用styles.xml属性colorAccent中设置的颜色。
The background color of this view defaults to the your theme's colorAccent. 此视图的背景颜色默认为主题的colorAccent。 If you wish to change this at runtime then you can do so via setBackgroundTintList(ColorStateList). 如果您希望在运行时更改此设置,则可以通过setBackgroundTintList(ColorStateList)执行此操作。
If you wish to change the color 如果你想改变颜色
As @Dantalian mentioned in the comments, if you wish to change the icon color, you can use 正如@Dantalian在评论中提到的,如果你想改变图标颜色,你可以使用
android:tint="@color/white"
Vijet Badigannavar's answer is correct but using ColorStateList
is usually complicated and he didn't tell us how to do it. Vijet Badigannavar的答案是正确的,但使用ColorStateList
通常很复杂,他没有告诉我们该怎么做。 Since we often focus on changing View
's color in normal and pressed state, I'm going to add more details: 由于我们经常专注于在正常和按下状态下改变View
的颜色,我将添加更多细节:
If you want to change FAB
's color in normal state, you can just write 如果你想在正常状态下改变FAB
的颜色,你可以写
mFab.setBackgroundTintList(ColorStateList.valueOf(your color in int));
If you want to change FAB
's color in pressed state, thanks for Design Support Library 22.2.1 , you can just write 如果你想在按下状态下改变FAB
的颜色,感谢Design Support Library 22.2.1 ,你可以写
mFab.setRippleColor(your color in int);
By setting this attribute, when you long-pressed the FAB
, a ripple with your color will appear at your touch point and reveal into whole surface of FAB
. 通过设置此属性,当您长按FAB
,您的触摸点会出现带有颜色的波纹并显示在FAB
整个表面上。 Please notice that it won't change FAB
's color in normal state. 请注意,它不会在正常状态下改变FAB
的颜色。 Below API 21(Lollipop), there is no ripple effect but FAB
's color will still change when you're pressing it. 在API 21(棒棒糖)下面,没有涟漪效应,但当你按下它时, FAB
的颜色仍会改变。
Finally, if you want to implement more complex effect for states, then you should dig deeply into ColorStateList
, here is a SO question discussing it: 最后,如果你想为状态实现更复杂的效果,那么你应该深入研究ColorStateList
,这是一个讨论它的SO问题: . 。
UPDATE: Thanks for @Kaitlyn's comment. 更新:感谢@ Kaitlyn的评论。 To remove stroke of FAB using backgroundTint as its color, you can set app:borderWidth="0dp"
in your xml. 要使用backgroundTint作为颜色删除FAB的笔划,可以在xml中设置app:borderWidth="0dp"
。
if you try to change color of FAB by using app, there some problem. 如果您尝试使用应用程序更改FAB的颜色,则存在一些问题。 frame of button have different color, so what you must to do: 按钮框有不同的颜色,所以你必须做的事情:
app:backgroundTint="@android:color/transparent"
and in code set the color: 并在代码中设置颜色:
actionButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.white)));
转载地址:http://ppcnb.baihongyu.com/