Java Android 更改浮动操作按钮颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30969455/
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
Android changing Floating Action Button color
提问by Jjang
I have been trying to change Material's Floating Action Button color, but without success.
我一直在尝试更改 Material 的 Floating Action Button 颜色,但没有成功。
<android.support.design.widget.FloatingActionButton
android:id="@+id/profile_edit_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/ic_mode_edit_white_24dp" />
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 proposed duplicate question, but none of them works; the button remained green and also became a square.
但以上都没有奏效。我也尝试过提出的重复问题中的解决方案,但没有一个有效;按钮保持绿色,也变成了正方形。
P.S. It would be also nice to know how to add ripple effect, couldn't understand that either.
PS 知道如何添加涟漪效果也很好,也无法理解。
采纳答案by Marko
As described in the documentation, by default it takes the color set in styles.xmlattribute colorAccent.
由于在所描述的文档,默认情况下需要在颜色集合styles.xml属性colorAccent。
The background color of this view defaults to the your theme's colorAccent. If you wish to change this at runtime then you can do so via setBackgroundTintList(ColorStateList).
此视图的背景颜色默认为您主题的 colorAccent。如果您希望在运行时更改它,那么您可以通过 setBackgroundTintList(ColorStateList) 来实现。
If you wish to change the color
如果你想改变颜色
- in XML with attribute app:backgroundTint
- 在带有属性app:backgroundTint 的XML 中
<android.support.design.widget.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add"
app:backgroundTint="@color/orange"
app:borderWidth="0dp"
app:elevation="6dp"
app:fabSize="normal" >
- in code with .setBackgroundTintList(answer below by ywwynm)
- 在带有.setBackgroundTintList 的代码中(下面由ywwynm回答)
As @Dantalian mentioned in the comments, if you wish to change the icon color for Design Support Library up to v22 (inclusive), you can use
正如@Dantalian 在评论中提到的,如果您希望将 Design Support Library 的图标颜色更改为 v22 (inclusive),您可以使用
android:tint="@color/white"
For Design Support Library since v23for you can use:
对于自 v23 以来的设计支持库,您可以使用:
app:tint="@color/white"
Also with androidX
libraries you need to set a 0dp border in your xml layout:
此外,对于androidX
库,您需要在 xml 布局中设置 0dp 边框:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add"
app:backgroundTint="@color/orange"
app:borderWidth="0dp"
app:elevation="6dp"
app:fabSize="normal" />
回答by tachyonflux
The FAB is colored based on your colorAccent
.
FAB 根据您的colorAccent
.
<style name="AppTheme" parent="Base.Theme.AppCompat.Light">
<item name="colorAccent">@color/accent</item>
</style>
回答by Vijet Badigannavar
The document suggests that it takes the @color/accent by default. But we can override it on code by using
该文档建议默认使用@color/accent。但是我们可以通过使用在代码上覆盖它
fab.setBackgroundTintList(ColorStateList)
Also remember,
还要记住,
The minimum API version to use this library is 15 so you need to update it! if you dont want to do it then you need to define a custom drawable and decorate it!
使用这个库的最低 API 版本是 15,所以你需要更新它!如果您不想这样做,那么您需要定义一个自定义可绘制对象并对其进行装饰!
回答by ywwynm
Vijet Badigannavar's answer is correct but using ColorStateList
is usually complicated and he didn't tell us how to do it. Since we often focus on changing View
's color in normal and pressed state, I'm going to add more details:
Vijet Badigannavar 的回答是正确的,但使用ColorStateList
通常很复杂,他没有告诉我们如何去做。由于我们经常关注View
在正常和按下状态下更改的颜色,因此我将添加更多细节:
If you want to change
FAB
's color in normal state, you can just writemFab.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 writemFab.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 ofFAB
. Please notice that it won't changeFAB
's color in normal state. Below API 21(Lollipop), there is no ripple effect butFAB
's color will still change when you're pressing it.
如果你想改变
FAB
正常状态下的颜色,你可以写mFab.setBackgroundTintList(ColorStateList.valueOf(your color in int));
如果你想改变
FAB
按下状态的颜色,感谢 Design Support Library 22.2.1,你可以写mFab.setRippleColor(your color in int);
通过设置此属性,当您长按 时
FAB
,您的触摸点会出现带有您颜色的波纹,并显示到 的整个表面FAB
。请注意,它FAB
在正常状态下不会改变颜色。在 API 21(Lollipop) 以下,没有涟漪效应,但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: How do I create ColorStateList programmatically?.
最后,如果你想为状态实现更复杂的效果,那么你应该深入研究ColorStateList
,这是一个讨论它的问题:如何以编程方式创建 ColorStateList?.
UPDATE:Thanks for @Kaitlyn's comment. To remove stroke of FAB using backgroundTint as its color, you can set app:borderWidth="0dp"
in your xml.
更新:感谢@Kaitlyn 的评论。要使用 backgroundTint 作为颜色删除 FAB 的笔触,您可以app:borderWidth="0dp"
在 xml 中设置。
回答by ergo
if you try to change color of FAB by using app, there some problem. frame of button have different color, so what you must to do:
如果您尝试使用应用程序更改 FAB 的颜色,则会出现一些问题。按钮的框架有不同的颜色,所以你必须做的是:
app:backgroundTint="@android:color/transparent"
and in code set the color:
并在代码中设置颜色:
actionButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.white)));
回答by Gaurav Kumar Gupta
Thanks to autocomplete. I got lucky after a few hit and trials:
感谢自动完成。经过几次打击和试验后,我很幸运:
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:backgroundTint="@color/whicheverColorYouLike"
-- or -- (both are basically the same thing)
-- 或 -- (两者基本上是一回事)
xmlns:app="http://schemas.android.com/apk/res-auto"
app:backgroundTint="@color/whicheverColorYouLike"
This worked for me on API Version 17 with design library 23.1.0.
这在带有设计库 23.1.0 的 API 版本 17 上对我有用。
回答by Robin Davies
Other solutions may work. This is the 10 pound gorilla approach that has the advantage of being broadly applicable in this and similar cases:
其他解决方案可能有效。这是 10 磅重的大猩猩方法,其优点是可广泛适用于这种情况和类似情况:
Styles.xml:
样式.xml:
<style name="AppTheme.FloatingAccentButtonOverlay" >
<item name="colorAccent">@color/colorFloatingActionBarAccent</item>
</style>
your layout xml:
你的布局xml:
<android.support.design.widget.FloatingActionButton
android:theme="AppTheme.FloatingAccentButtonOverlay"
...
</android.support.design.widget.FloatingActionButton>
回答by HenriqueMS
As Vasil Valchevnoted in a comment it is simpler than it looks, but there is a subtle difference that I wasn't noticing in my XML.
正如Vasil Valchev在评论中指出的那样,它比看起来更简单,但我在 XML 中没有注意到其中的细微差别。
<android.support.design.widget.FloatingActionButton
android:id="@+id/profile_edit_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/ic_mode_edit_white_24dp"
app:backgroundTint="@android:color/white"/>
Notice it is:
注意是:
app:backgroundTint="@android:color/white"
and not
而不是
android:backgroundTint="@android:color/white"
回答by Ashana.Hymanol
just use,
就用,
app:backgroundTint="@color/colorPrimary"
dont use,
不要使用,
android:backgroundTint="@color/colorPrimary"
回答by Purvik Rana
I got the same problem and its all snatching my hair. Thanks for this https://stackoverflow.com/a/35697105/5228412
我遇到了同样的问题,而且都在抓我的头发。感谢这个 https://stackoverflow.com/a/35697105/5228412
What we can do..
我们能做什么..
favourite_fab.setImageDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.favourite_selected));
it works fine for me and wish for others who'll reach here.
它对我来说很好,并希望其他人能到达这里。