如何更改浮动操作按钮颜色?

问题描述 投票:0回答:8
fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(PrivateDealActivity.this, R.color.colorWhite)));

这不起作用。如何更改浮动操作按钮的背景颜色

android material-design floating-action-button material-components-android material-components
8个回答
7
投票

使用 material 组件

FloatingActionButton
material 主题 默认情况下,它使用具有属性
colorSecondary
的颜色。

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

  • 您可以在 xml 中使用
    app:backgroundTint
    属性:
<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>

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*));
}

3
投票

请尝试这个。

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

2
投票

只需使用此属性:

android:backgroundTint="@color/yourColor"

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));

在这里您可以使用 Color.parseColor()

ContextCompat.getColor()
替换
required_color


0
投票

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

Android开发者-FloatingActionButton中所述:

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

如果无法根据项目的要求更改您的颜色强调,但您需要以编程方式实现此功能,您可以发布您的代码,我将非常乐意更新我的答案。


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

0
投票

就我而言,我只是改变风格 从

style="@style/Widget.MaterialComponents.FloatingActionButton"
style="@style/Widget.Design.FloatingActionButton"

并添加后台手册

app:backgroundTint="@color/yourColor"

<com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab_contact"
            style="@style/Widget.Design.FloatingActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:backgroundTint="@color/colorTextCoba"
            app:fabSize="normal"
            app:layout_anchorGravity="bottom|right|end"
             />
© www.soinside.com 2019 - 2024. All rights reserved.