【问题标题】:Floating Action Button Transparent above 5.0.15.0.1以上透明浮动操作按钮
【发布时间】:2016-01-17 08:26:10
【问题描述】:

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

动态改变 TintList 颜色

 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>

【问题讨论】:

  • 你可以为 FloatingActionButton 发布你的 xml 吗?
  • @MukeshRana 现已添加
  • 能否同时为 5.0.1 前后的版本添加 styles.xml?
  • @MukeshRana 现已添加
  • 那么 values 和 values-21 是一样的吗?

标签: android floating-action-button


【解决方案1】:

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

  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;

}

希望这能帮助遇到与我相同问题的其他人

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 2016-01-13
    • 2018-10-18
    相关资源
    最近更新 更多