11
fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(PrivateDealActivity.this, R.color.colorWhite)));

这是行不通的。如何更改浮动操作按钮的背景颜色

4

7 回答 7

6

默认情况下使用材质组件 FloatingActionButton材质主题,它使用带有属性的颜色colorSecondary

要更改此颜色,您有不同的选择:

  • 您可以app:backgroundTint在 xml 中使用该属性:
<com.google.android.material.floatingactionbutton.FloatingActionButton
       ...
       app:backgroundTint=".." />
  • 您可以使用<item name="backgroundTint">属性使用自定义样式
  <!--<item name="floatingActionButtonStyle">@style/Widget.MaterialComponents.FloatingActionButton</item> -->
  <style name="MyFloatingActionButton" parent="@style/Widget.MaterialComponents.FloatingActionButton">
    <item name="backgroundTint">....</item>
  </style>
  • 从材料组件的1.1.0版本开始,您还可以使用新materialThemeOverlay属性仅覆盖组件的默认颜色colorSecondary
  <style name="MyFloatingActionButton" parent="@style/Widget.MaterialComponents.FloatingActionButton">
    <item name="materialThemeOverlay">@style/MyFabOverlay</item>
  </style>

  <style name="MyFabOverlay">
    <item name="colorSecondary">@color/custom2</item>
  </style>
于 2019-09-01T20:54:42.457 回答
3

请试试这个。

app:backgroundTint="@android:color/darker_gray"
于 2018-11-13T11:07:55.293 回答
3

您可以使用此代码更改背景颜色:

FloatingActionButton button = findViewById(R.id.your_button);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP && button instanceof AppCompatButton) {
    ((AppCompatButton) button).setSupportBackgroundTintList(ColorStateList.valueOf(*your color in integer*));
} else {
    ViewCompat.setBackgroundTintList(button, ColorStateList.valueOf(*your color in integer*));
}
于 2018-07-03T12:55:01.297 回答
2

只需使用此属性:

android:backgroundTint="@color/yourColor"
于 2019-07-10T05:01:45.190 回答
2

你的代码是完全正确的。我还以类似的方式编写了以编程方式更改浮动操作按钮的背景颜色。只需检查一次您的 xml 代码。我的 XML 代码:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    app:borderWidth="0dp"
    android:id="@+id/fab"
    android:layout_margin="16dp"
    app:elevation="8dp"/>

请记住,保持borderWidth="0dp"很重要。现在,在 Java 代码中:

fab = view.findViewById(R.id.fab);
fab.setBackgroundTintList(ColorStateList.valueOf(required_color));

在这里,您可以使用or替换required_colorColor.parseColor()ContextCompat.getColor()

于 2020-10-31T19:33:06.190 回答
0

解决您的问题的一个简单方法是更改​​您的默认 colorAccent。

Android Developers-FloatingActionButton所述:

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

如果无法更改您的颜色口音,关于您的项目的要求,但您需要以编程方式实现这一点,您可以发布您的代码,我将非常乐意更新我的答案。

于 2018-07-03T10:40:48.793 回答
0

你可以使用

app:backgroundTint="@color/colorAccent"

像下面

<android.support.design.widget.FloatingActionButton
        android:id="@+id/add_student_house"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:backgroundTint="@color/colorAccent"
        android:src="@drawable/ic_house_add"/>

请注意它是app:backgroundTint不是android:backgroundTint

你也可以通过添加样式来做到这一点:

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

并在浮动操作按钮中将此样式作为主题,如下所示:

android:theme="@style/AppTheme.FloatingActionButton"

<android.support.design.widget.FloatingActionButton
    android:id="@+id/add_student_house"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.FloatingActionButton"
    android:src="@drawable/ic_house_add"/>
于 2019-07-10T04:24:28.493 回答