使用 setBackgroundTintList 设置 FloatingActionButton 的背景颜色

Setting the background color of FloatingActionButton with setBackgroundTintList

我遇到一个问题,通过在设计库中设置 FloatingActionButton 的背景,屏幕上出现的任何其他 FloatingActionButton 视图都会采用与原来的。

左边是应用在设备运行 API 21上的截图,右边是设备运行 API 22和FloatingActionButtons 尽管每个人的颜色设置不同,但它们的颜色都相同。

我已经在空白应用程序中对此进行了测试。这是 activity_main 的 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            tools:context=".MainActivity">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_2"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_margin="16dp"
        app:borderWidth="0dp"
        app:elevation="4dp"
        app:fabSize="normal" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_1"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:layout_toLeftOf="@id/fab_2"
        app:borderWidth="0dp"
        app:elevation="4dp"
        app:fabSize="normal" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_3"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:layout_toRightOf="@id/fab_2"
        app:borderWidth="0dp"
        app:elevation="4dp"
        app:fabSize="normal" />

</RelativeLayout>

这里是MainActivity的相关代码。除此之外,应用程序中不存在其他代码。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Setting colors of first fab
    FloatingActionButton fab =
            (FloatingActionButton) findViewById(R.id.fab_1);
    setFloatingActionButtonColors(fab,
            getResources().getColor(R.color.blue),
            getResources().getColor(R.color.blue_dark));

    //Setting colors of second fab
    fab = (FloatingActionButton) findViewById(R.id.fab_2);
    setFloatingActionButtonColors(fab,
            getResources().getColor(R.color.red),
            getResources().getColor(R.color.red_dark));

    //Setting colors of third fab
    fab = (FloatingActionButton) findViewById(R.id.fab_3);
    setFloatingActionButtonColors(fab,
            getResources().getColor(R.color.green),
            getResources().getColor(R.color.green_dark));
}

private void setFloatingActionButtonColors(FloatingActionButton fab, int primaryColor, int rippleColor) {
    int[][] states = {
            {android.R.attr.state_enabled},
            {android.R.attr.state_pressed},
    };

    int[] colors = {
            primaryColor,
            rippleColor,
    };

    ColorStateList colorStateList = new ColorStateList(states, colors);
    fab.setBackgroundTintList(colorStateList);
}

最后,我用上面的代码创建了一个简单的Android Studio 项目,可以找到here。项目中还有一些额外的异常,如果你想自己测试一下。

有没有人遇到过这样的问题?这是一个已知错误,还是有解决方法?

所以看起来这是 Android API 22 的错误。我已经向 Android 问题跟踪器提交了错误报告。门票可以找到here.

目前,我没有解决方法,但如果我找到了,会报告回来。

在 google https://code.google.com/p/android/issues/detail?id=201873

上报告了类似的问题

但在 Android 支持库发布后,修订版 23.2.1(2016 年 3 月)此错误已解决。

FloatingActionButton.setBackgroundTintList(@Nullable ColorStateList tint) 不再更改背景颜色

将支持库更新为 Android Support Library to 23.2.1