【问题标题】:How to change floating action button color?如何更改浮动操作按钮的颜色?
【发布时间】:2018-12-11 15:19:06
【问题描述】:
fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(PrivateDealActivity.this, R.color.colorWhite)));

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

【问题讨论】:

  • 请将您的代码标记为代码,并说明您期望发生的情况以及实际发生的情况。
  • 使用 xml:app:backgroundTint="YOUR_COLOR_CODE" 以编程方式使用:floatingButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor("YOUR_COLOR_CODE")));
  • 请确保您使用的是android.support.design.widget.FloatingActionButton
  • floatingButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor("YOUR_COLOR_CODE"))); //这段代码对我不起作用。

标签: android material-design floating-action-button material-components-android material-components


【解决方案1】:

使用 材质组件 FloatingActionButtonmaterial theme 默认情况下,它使用属性为 colorSecondary 的颜色。

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

  • 您可以在xml中使用app:backgroundTint属性:
<com.google.android.material.floatingactionbutton.FloatingActionButton
       ...
       app:backgroundTint=".." />
  • 您可以通过 &lt;item name="backgroundTint"&gt; 属性使用自定义样式
  <!--<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>

【讨论】:

  • 它工作得很好,但它改变了其他按钮属性,例如文本样式(所有大写字母和字母间距,当然还有其他)。你有解决办法吗?
  • @SebastienRieu 为什么要更改其他属性?您正在使用 parent="@style/Widget.MaterialComponents.FloatingActionButton" 扩展当前样式
  • 你是对的。这是因为我的按钮是 ExtendedFloatingActionButton 所以我必须应用相应的样式。 @style/Widget.MaterialComponents.ExtendedFloatingActionButton。谢谢!
【解决方案2】:

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

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"
    

    【讨论】:

      【解决方案4】:

      只需使用这个属性:

      android:backgroundTint="@color/yourColor"
      

      【讨论】:

        【解决方案5】:

        您的代码完全正确。我还以类似的方式编写了更改浮动操作按钮的背景颜色以编程方式。只需检查一次您的 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

        【讨论】:

          【解决方案6】:

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

          Android Developers-FloatingActionButton 所述:

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

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

          【讨论】:

          • OP 正在使用setBackgroundTintList,但它不起作用......所以这个答案没有帮助
          【解决方案7】:

          你可以使用

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

          【讨论】:

            猜你喜欢
            • 2015-09-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-01-01
            • 2020-01-21
            • 1970-01-01
            相关资源
            最近更新 更多