浮动操作按钮 在 5.0.1 以上透明



Android 浮动操作按钮在操作系统版本 5.0.1 之前工作正常。但它无法正常工作,而是在操作系统版本 5.0.1 以上变得透明。有没有人遇到过这样的问题。我必须动态更改背景色调列表,因此仅在 xml 中定义不会使用完整。那么如何使用5.0.1以上的操作系统来处理它。提前感谢您的合作。

动态更改色调列表颜色

 mFloatingActionButtonBack.setBackgroundTintList(changeColor(getResources().getColor(R.color.color_gray)));

用于浮动操作按钮的 XML

<android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/contact_floating_btn"
        app:fabSize="normal"
        android:src="@drawable/contact_directions"
        android:layout_alignParentRight="true"
        android:clickable="true"
        android:layout_below="@+id/gmap_frag"
        android:layout_marginTop="@dimen/fab_margin"
        android:layout_marginRight="@dimen/fab_margin_right"
        />

样式部分

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/color_primary</item>
    <item name="colorPrimaryDark">@color/color_primary_dark</item>
    <item name="colorAccent">@color/color_primary</item>
    <item name="colorControlNormal">#d7d7d7</item>
</style>

我找到的解决我所面临问题的方法如下。

  if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Fab.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.color_gray)));
        } else {
            Fab.setBackgroundTintList(changeColor(getActivity().getResources().getColor(R.color.color_gray)));
        }

ColorStateList的方法是

 public ColorStateList changeColor(int color){
    ColorStateList myColorStateList = new ColorStateList(
            new int[][]{
                    new int[]{android.R.attr.state_active},
                    new int[]{android.R.attr.state_window_focused},
                    new int[]{android.R.attr.state_pressed}, //1
                    new int[]{android.R.attr.state_focused}, //2
                    new int[]{android.R.attr.state_focused, android.R.attr.state_pressed} //3
            },
            new int[]{
                    color,
                    color,
                    color, //1
                    color, //2
                    color//3
            }
    );
    return myColorStateList;
}

希望这能帮助其他人与我有同样的问题

最新更新