Android更改浮动操作按钮颜色

时间:2015-06-21 21:20:22

标签: java android android-5.0-lollipop 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" />

我试图添加

android:background="@color/mycolor"

或通过代码

FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.profile_edit_fab);
fab.setBackgroundColor(Color.parseColor("#mycolor"));

fab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#mycolor")));

但以上都没有奏效。我也尝试了“重复问题”中的解决方案,但它们都不起作用,按钮保持绿色并且也变成了正方形。

我想要一个详细的答案,谢谢。

P.S。知道如何添加涟漪效应也是很好的,也无法理解。

25 个答案:

答案 0 :(得分:917)

documentation所述,默认情况下,它采用 styles.xml 属性 colorAccent 中设置的颜色。

  

此视图的背景颜色默认为主题的colorAccent。如果您希望在运行时更改此设置,则可以通过setBackgroundTintList(ColorStateList)进行更改。

如果您想更改颜色

  • 在XML中,属性为 app:backgroundTint
<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" >
    .setBackgroundTintList 的代码中
  • (由ywwynm回答)

正如@Dantalian在评论中提到的,如果你想改变图标颜色,你可以使用

android:tint="@color/white"     

答案 1 :(得分:218)

Vijet Badigannavar的回答是正确的,但使用ColorStateList通常很复杂,他没有告诉我们该怎么做。由于我们经常专注于在正常和按下状态下更改View的颜色,我将添加更多细节:

  1. 如果你想在正常状态下改变FAB的颜色,你可以写

    mFab.setBackgroundTintList(ColorStateList.valueOf(your color in int));
    
  2. 如果您想在按下状态下更改FAB的颜色,感谢设计支持库 22.2.1 ,您只需编写

    mFab.setRippleColor(your color in int);
    

    通过设置此属性,当您长按FAB时,您的颜色的波纹将出现在您的触摸点并显示在FAB的整个表面中。请注意,在正常状态下它不会改变FAB的颜色。在API 21(Lollipop)下面,没有涟漪效应,但当你按下它时,FAB的颜色仍会改变。

  3. 最后,如果你想为状态实现更复杂的效果,那么你应该深入研究ColorStateList,这是一个讨论它的SO问题:How do I create ColorStateList programmatically?

    <强>更新 感谢@ Kaitlyn的评论。要使用backgroundTint作为颜色删除FAB的笔划,可以在xml中设置app:borderWidth="0dp"

答案 2 :(得分:108)

正如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"/>

注意它是:

app:backgroundTint="@android:color/white"

android:backgroundTint="@android:color/white"

答案 3 :(得分:51)

如果您尝试使用app更改FAB的颜色,则会出现一些问题。 按钮框有不同的颜色,所以你必须要做的事情:

app:backgroundTint="@android:color/transparent"

并在代码中设置颜色:

actionButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.white)));

答案 4 :(得分:44)

只需使用,

app:backgroundTint="@color/colorPrimary"

不要使用,

android:backgroundTint="@color/colorPrimary"

答案 5 :(得分:27)

FAB根据您的colorAccent进行着色。

<style name="AppTheme" parent="Base.Theme.AppCompat.Light">
    <item name="colorAccent">@color/accent</item>
</style>

答案 6 :(得分:19)

mFab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(mContext,R.color.mColor)));

答案 7 :(得分:12)

其他解决方案可能有效。这是10磅的大猩猩方法,具有广泛适用于此类和类似情况的优点:

Styles.xml:

<style name="AppTheme.FloatingAccentButtonOverlay" >
    <item name="colorAccent">@color/colorFloatingActionBarAccent</item>
</style>

你的布局xml:

<android.support.design.widget.FloatingActionButton
       android:theme="AppTheme.FloatingAccentButtonOverlay"
       ...
 </android.support.design.widget.FloatingActionButton>

答案 8 :(得分:10)

该文档表明它默认采用@ color / accent。但我们可以使用

覆盖代码
fab.setBackgroundTintList(ColorStateList)

还要记住,

使用此库的最低API版本为15,因此您需要更新它!如果您不想这样做,那么您需要定义一个自定义drawable并装饰它!

答案 9 :(得分:6)

 <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    app:elevation="6dp"
    app:backgroundTint="@color/colorAccent"
    app:pressedTranslationZ="12dp"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/add"/>

请注意,您在res / values / color.xml中添加颜色 并在您的工厂中包含该属性

   app:backgroundTint="@color/addedColor"

答案 10 :(得分:6)

感谢自动填充功能。经过几次打击和试验后,我很幸运:

    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    card_view:backgroundTint="@color/whicheverColorYouLike"

- 或 - (两者基本相同)

    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:backgroundTint="@color/whicheverColorYouLike"

这适用于API版本17和设计库23.1.0。

答案 11 :(得分:5)

我遇到了同样的问题而且它全都抢了我的头发。谢谢你  https://stackoverflow.com/a/35697105/5228412

我们能做什么..

 favourite_fab.setImageDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.favourite_selected));

它对我来说很好,并希望有人能够到达这里。

答案 12 :(得分:4)

使用下面的行

更改浮动操作按钮背景颜色
app:backgroundTint="@color/blue"

更改浮动操作按钮图标颜色

android:tint="@color/white"     

答案 13 :(得分:3)

使用Data Binding时,您可以执行以下操作:

android:backgroundTint="@{item.selected ? @color/selected : @color/unselected}"

我做了一个非常简单的example

答案 14 :(得分:2)

我是这样做的  机器人:背景= “@颜色/ colorAccent” 我只是去文件夹res然后点击文件夹值,然后点击colors.xml中的colors.xml我只是改变colorAccent的颜色,并在android:background和它完成中调用它

答案 15 :(得分:2)

默认情况下,使用Material Theme材料成分 FloatingActionButton时,它将采用在styles.xml属性 colorSecondary中设置的颜色

  • 您可以在xml中使用 app:backgroundTint 属性:
<com.google.android.material.floatingactionbutton.FloatingActionButton
       ...
       app:backgroundTint=".."
       app:srcCompat="@drawable/ic_plus_24"/>
  • 您可以使用fab.setBackgroundTintList();

  • 您可以使用 <item name="backgroundTint"> 属性

  • 自定义样式
  <!--<item name="floatingActionButtonStyle">@style/Widget.MaterialComponents.FloatingActionButton</item> -->
  <style name="MyFloatingActionButton" parent="@style/Widget.MaterialComponents.FloatingActionButton">
    <item name="backgroundTint">#00f</item>
    <!-- color used by the icon -->
    <item name="tint">@color/...</item>
  </style>
  • 从材料组件的版本 1.1.0 开始,您可以使用新的 materialThemeOverlay 属性来覆盖某些组件的默认颜色:
  <style name="MyFloatingActionButton" parent="@style/Widget.MaterialComponents.FloatingActionButton">
    <item name="materialThemeOverlay">@style/MyFabOverlay</item>
  </style>

  <style name="MyFabOverlay">
    <item name="colorSecondary">@color/custom2</item>
    <!-- color used by the icon -->
    <item name="colorOnSecondary">@color/...</item>
  </style>

enter image description here

答案 16 :(得分:1)

我们缺少的一点是,在按钮上设置颜色之前,处理此颜色所需的值非常重要。所以你可以去价值观&gt;颜色。您将找到默认值,但您也可以通过粘贴和粘贴它们来创建颜色,更改颜色和名称。然后......当你去改变浮动按钮的颜色(在activity_main中)时,你可以选择你创建的那个

例证 - 值的代码&gt;默认颜色的颜色+我创造的3种颜色:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

    <color name="corBotaoFoto">#f52411</color>
    <color name="corPar">#8e8f93</color>
    <color name="corImpar">#494848</color>

</resources>

现在我的浮动操作按钮带有我创建的颜色并命名为“corPar”:

<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"
        android:src="@android:drawable/ic_input_add"
        android:tint="#ffffff"
        app:backgroundTint="@color/corPar"/>

它对我有用。祝你好运!

答案 17 :(得分:1)

材料1.1.0中Floating Action Button的新主题属性映射

在您的应用主题中:

  • 设置colorSecondary为FAB的背景设置颜色(映射到backgroundTint)
  • 设置colorOnSecondary为FAB的图标/文本和波纹颜色设置颜色(映射到色调和波纹颜色)

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- ...whatever else you declare in your app theme.. -->
    <!-- Set colorSecondary to change background of FAB (backgroundTint) -->
    <item name="colorSecondary">@color/colorSecondary</item>
    <!-- Customize colorSecondary to change icon/text of FAB (maps to tint and rippleColor) -->
    <item name="colorOnSecondary">@android:color/white</item>
</style>

答案 18 :(得分:1)

如果您的浮动操作按钮没有可绘制的对象,则可以使用以下方法以编程方式更改色调:

fab.getBackground().mutate().setTint(ContextCompat.getColor(yourContext, R.color.anyColor));

答案 19 :(得分:1)

对于Material design,我只是这样更改了浮动操作按钮的颜色, 在“浮动操作”按钮xml中添加以下两行。 完成

 android:backgroundTint="@color/colorPrimaryDark"
 app:borderWidth="0dp"

答案 20 :(得分:0)

如果您想更改代码,可以使用它 floating.setBackgroundTintList(getResources()getColorStateList(R.color.vermelho));

答案 21 :(得分:0)

add colors in color.xml file

在color.xml文件中添加颜色,然后添加此行代码... floatingActionButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.fab2_color)));

答案 22 :(得分:0)

使用

中的

app:backgroundTint =“ @ color / orange”


<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/id_share_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/share"
        app:backgroundTint="@color/orange"
        app:fabSize="mini"
        app:layout_anchorGravity="end|bottom|center" />



</androidx.coordinatorlayout.widget.CoordinatorLayout>

答案 23 :(得分:0)

在科特林:

A

答案 24 :(得分:0)

我的解决方案,适用于我的数据绑定

val color = ContextCompat.getColor(context, R.color.colorPrimary)
binding.fab.backgroundTintList = ColorStateList.valueOf(getColor)
相关问题