博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android更改浮动操作按钮颜色
阅读量:2290 次
发布时间:2019-05-09

本文共 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知道如何添加涟漪效果也不错,也无法理解。


#1楼

参考:


#2楼

The FAB is colored based on your colorAccent . FAB根据您的colorAccent着色。


#3楼

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并装饰它!


#4楼

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 如果你想改变颜色

  • in XML with attribute app:backgroundTint 在XML中使用属性app:backgroundTint
  • in code with .setBackgroundTintList (answer below by ) .setBackgroundTintList的代码中(由回答如下)

As @Dantalian mentioned in the comments, if you wish to change the icon color, you can use 正如@Dantalian在评论中提到的,如果你想改变图标颜色,你可以使用

android:tint="@color/white"

#5楼

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的颜色,我将添加更多细节:

  1. If you want to change FAB 's color in normal state, you can just write 如果你想在正常状态下改变FAB的颜色,你可以写

    mFab.setBackgroundTintList(ColorStateList.valueOf(your color in int));
  2. 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"


#6楼

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/

你可能感兴趣的文章
MySQL设置及修改root密码
查看>>
MySQL忘记密码重置管理员密码
查看>>
MySQL创建及授权账号
查看>>
MySQL库的基本操作
查看>>
MySQL表的基本操作
查看>>
MySQL数据类型
查看>>
MySQL SQL语句最常见的分类
查看>>
MySQL用户权限
查看>>
MySQL数据备份
查看>>
MySQL使用explain检查索引执行计划
查看>>
MySQL字符集
查看>>
MySQL存储引擎
查看>>
MySQL主从同步
查看>>
MySQL半同步复制
查看>>
MySQL主库宕机从库提权
查看>>
MySQL主主模式
查看>>
MySQL错误代码
查看>>
MySQL binlog的三种模式
查看>>
MySQL利用binlog增量恢复数据库
查看>>
Tomcat多实例多应用
查看>>