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

644

我一直在尝试更改 Material 的浮动操作按钮颜色,但没有成功。

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

但是上述方法都没有奏效。我还尝试了提出的重复问题中的解决方案,但它们都无法正常工作;该按钮仍然保持绿色,并且变成了一个正方形。

另外,知道如何添加涟漪效果也会很好,但我也没搞懂怎么做。


涟漪效果在早期的安卓设备上不可用,因为它使用了新的RenderThread - tachyonflux
@karaokyo 好的,但我该如何做呢? - Jjang
52
我认为谷歌在让这些东西易于使用方面做得很差。 - Joop
要以编程方式实现向后兼容,请参见https://dev59.com/D10Z5IYBdhLWcg3w4jhy#38618011 - Ralph Pina
28个回答

3

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

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

2

Kotlin中:

val gray = getColor(requireContext(), R.color.green)
binding.fabSubmit.backgroundTintList = ColorStateList.valueOf(gray)

1
您可以使用扩展,所以将app:iconTint设置为如下内容:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:id="@+id/fAB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       
        app:icon="@drawable/d0"
        app:iconTint="@color/white"
         />

1

你可以通过以下代码在 Kotlin 中实现这一点。

 binding.fabAddPostMenu.backgroundTintList =
    ColorStateList.valueOf(ContextCompat.getColor(this,R.color.complaint_red))

0

如果您想以编程方式更改颜色,可以使用此代码

floating.setBackgroundTintList(getResources().getColorStateList(R.color.vermelho));

0

add colors in color.xml file

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

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>


0
在styles.xml中添加:
<style name="FloatButtonSytle" parent="Widget.MaterialComponents.FloatingActionButton">
    <item name="colorAccent">@color/main_gradient_end</item>
</style>

在你的FloatingActionButton元素中添加:
style="@style/FloatButtonSytle"

像这样:
<com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/aparat_button"
            style="@style/FloatButtonSytle"
            android:layout_width="75dp"
            android:layout_height="75dp"
            />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接