Android的变化浮动操作按钮颜色按钮、颜色、操作、Android

2023-09-12 11:04:37 作者:爷.独霸天下

一直试图小时,以改变材料的浮动操作按钮(FAB)的颜色,但没有成功。

Been trying for hours to change Material's Floating Action Button (fab) color but without success.

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

或通过code

or via code

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

或代替

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

以上都不是工作的。巴顿依然绿,也变成了方形。

None of the above worked. Button remained green and also became square.

我想了详细的解答,谢谢。

I'd like a detailed answer, thanks.

诗这将是很好,知道如何添加连锁反应,想不明白,要么。

P.s It would be nice to know how to add ripple effect, couldn't understand that either.

试图在重复问题的解决方案,但没有工作!

Tried the solutions in the "duplicate question" but none works!

推荐答案

正如documentation,它需要的颜色集 styles.xml 属性 colorAccent

As described in the documentation, it takes the color set in styles.xml attribute colorAccent.

这个观点默认为你的主题colorAccent的背景色。如果你想在运行时改变,那么你可以通过setBackgroundTintList(ColorStateList)这样做的。

The background color of this view defaults to the your theme's colorAccent. If you wish to change this at runtime then you can do so via setBackgroundTintList(ColorStateList).

如果你想改变颜色

在XML与属性应用:backgroundTint 在code是 .setBackgroundTintList in XML with attribute app:backgroundTint in code with .setBackgroundTintList
<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" >

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

As @Dantalian mentioned in the comments, if you wish to change the icon color, you can use

android:tint="@color/white"