首页 文章

Android更改浮动操作按钮颜色

提问于
浏览
404

一直试图改变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")));

但上述都没有奏效 . 我也尝试了"duplicate question"中的解决方案,但没有一个工作,按钮保持绿色,也成为一个正方形 .

我想详细解答一下,谢谢 .

附:知道如何添加涟漪效果也不错,也无法理解 .

18 回答

  • 843

    add colors in color.xml file

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

  • 205

    我们缺少的一点是,在按钮上设置颜色之前,处理此颜色所需的值非常重要 . 所以你可以去 Value 观>颜色 . 您将找到默认值,但您也可以通过粘贴和粘贴它们来创建颜色,更改颜色和名称 . 然后......当你去改变浮动按钮的颜色(在activity_main中)时,你可以选择你创建的那个

    例子 - 代码值>颜色和默认颜色我创建了3种颜色:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="colorPrimary">#3F51B5</color>
        <color name="colorPrimaryDark">#303F9F</color>
        <color name="colorAccent">#FF4081</color>
    
        <color name="corBotaoFoto">#f52411</color>
        <color name="corPar">#8e8f93</color>
        <color name="corImpar">#494848</color>
    
    </resources>
    

    现在我的浮动动作按钮具有我创建的颜色并命名为“corPar”:

    <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@android:drawable/ic_input_add"
            android:tint="#ffffff"
            app:backgroundTint="@color/corPar"/>
    

    它对我有用 . 祝好运!

  • 4

    FAB根据您的 colorAccent 进行着色 .

    <style name="AppTheme" parent="Base.Theme.AppCompat.Light">
        <item name="colorAccent">@color/accent</item>
    </style>
    
  • 9

    该文档表明它默认采用@ color / accent . 但是我们可以通过使用来覆盖代码

    fab.setBackgroundTintList(ColorStateList)
    

    还记得,

    使用此库的最低API版本为15,因此您需要更新它!如果您不想这样做,那么您需要定义一个自定义drawable并装饰它!

  • 11

    您可以使用它以防您想要更改代码floating.setBackgroundTintList(getResources() . getColorStateList(R.color.vermelho));

  • 39

    使用下面的行更改浮动操作按钮背景颜色

    app:backgroundTint="@color/blue"
    

    更改浮动操作按钮图标颜色

    android:tint="@color/white"
    
  • 14

    使用Data Binding时,您可以执行以下操作:

    android:backgroundTint="@{item.selected ? @color/selected : @color/unselected}"
    

    我做了一个非常简单的example

  • 2

    我遇到了同样的问题而且它全都抢了我的头发 . 谢谢你https://stackoverflow.com/a/35697105/5228412

    我们能做什么..

    favourite_fab.setImageDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.favourite_selected));
    

    它对我来说很好,并希望有人能够到达这里 .

  • 27
    mFab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(mContext,R.color.mColor)));
    
  • 44

    正如Vasil Valchev在评论中指出的那样,它比看起来更简单,但是我在XML中没有注意到一个微妙的区别 .

    <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"
        app:backgroundTint="@android:color/white"/>
    

    注意它是:

    app:backgroundTint="@android:color/white"
    

    not

    android:backgroundTint="@android:color/white"
    
  • 5

    如果您尝试使用应用程序更改FAB的颜色,则存在一些问题 . 按钮框架有不同的颜色,所以你必须做的事情:

    app:backgroundTint="@android:color/transparent"
    

    并在代码中设置颜色:

    actionButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.white)));
    
  • 1

    我这样做了android:background =“@ color / colorAccent”我只是去文件夹res然后点击文件夹值然后在colors.xml中的colors.xml我只是改变colorAccent的颜色并在android:background中调用它并完成了

  • 96

    其他解决方案也可行 . 这是10磅的大猩猩方法,具有广泛适用于此类和类似情况的优点:

    Styles.xml:

    <style name="AppTheme.FloatingAccentButtonOverlay" >
        <item name="colorAccent">@color/colorFloatingActionBarAccent</item>
    </style>
    

    你的布局xml:

    <android.support.design.widget.FloatingActionButton
           android:theme="AppTheme.FloatingAccentButtonOverlay"
           ...
     </android.support.design.widget.FloatingActionButton>
    
  • 5

    感谢自动完成 . 经过几次打击和试验后,我很幸运:

    xmlns:card_view="http://schemas.android.com/apk/res-auto"
        card_view:backgroundTint="@color/whicheverColorYouLike"
    
    • 或 - (两者基本相同)
    xmlns:app="http://schemas.android.com/apk/res-auto"
        app:backgroundTint="@color/whicheverColorYouLike"
    

    这适用于API版本17和设计库23.1.0 .

  • 0

    只是用,

    app:backgroundTint="@color/colorPrimary"
    

    不要用,

    android:backgroundTint="@color/colorPrimary"
    
  • 0

    Vijet Badigannavar的答案是正确的,但使用 ColorStateList 通常很复杂,他没有告诉我们该怎么做 . 由于我们经常专注于改变 View 's color in normal and pressed state, I' m来添加更多细节:

    • 如果你想在正常状态下改变 FAB 的颜色,你可以写
    mFab.setBackgroundTintList(ColorStateList.valueOf(your color in int));
    
    • 如果你想在按下状态下改变 FAB 的颜色,感谢设计支持库 22.2.1 ,你可以写
    mFab.setRippleColor(your color in int);
    

    通过设置此属性,当您长按 FAB 时,您的颜色的波纹将出现在您的触摸点并显示在 FAB 的整个表面 . 请注意,在正常状态下它不会改变 FAB 的颜色 . 在API 21(Lollipop)下面,没有涟漪效应,但是按下了它. FAB 's color will still change when you' .

    最后,如果你想为状态实现更复杂的效果,那么你应该深入研究 ColorStateList ,这是一个讨论它的SO问题:How do I create ColorStateList programmatically? .

    UPDATE: 感谢@ Kaitlyn的评论 . 要使用backgroundTint作为颜色删除FAB的笔划,可以在xml中设置 app:borderWidth="0dp" .

  • 1
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        app:elevation="6dp"
        app:backgroundTint="@color/colorAccent"
        app:pressedTranslationZ="12dp"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/add"/>
    

    请注意,您在res / values / color.xml中添加颜色,并在fab中包含该属性

    app:backgroundTint="@color/addedColor"
    
  • 0

    documentation中所述,默认情况下它采用 styles.xml 属性 colorAccent 中设置的颜色 .

    此视图的背景颜色默认为主题的colorAccent . 如果您希望在运行时更改此设置,则可以通过setBackgroundTintList(ColorStateList)执行此操作 .

    如果你想改变颜色

    XML中的

    • ,属性为 app:backgroundTint
    <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" >
    

    带有 .setBackgroundTintList 的代码

    • (以下通过ywwynm回答)

    正如@Dantalian所说在评论中,如果您想更改图标颜色,可以使用

    android:tint="@color/white"
    

相关问题