How to change FAB background color

后端 未结 4 1075
清歌不尽
清歌不尽 2021-01-07 17:23

I am using Floating Action Button and I want to change the background color.

Here is my code

    

        
相关标签:
4条回答
  • 2021-01-07 17:32

    First create a style in your styles.xml:

    <style name="PrimaryActionButton" parent="Theme.AppCompat.Light">
    <item name="colorAccent">@color/colorPrimary</item>
    </style>
    

    Then set the theme of your fab to this style:

    <android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_favorite_white_24dp"
    android:theme="@style/PrimaryActionButton"
    app:fabSize="normal"
    app:rippleColor="@color/colorAccent" />
    

    You can see full descrption in: Android: Floating Action button

    0 讨论(0)
  • 2021-01-07 17:40

    Declare following in your app style:

    <item name="colorAccent">@color/yourColor</ item> 
    

    cheers

    0 讨论(0)
  • 2021-01-07 17:44

    Please update your android support and android material design libraries in build.gradle.

    (Add please your build.gradle file above)

    According to these sites:

    • [CodePath] Floating Action Buttons,
    • [Big Nerd Ranch] Floating Action Buttons in Android Lollipop

    and Android Developers reference you should use only:

       android:backgroundTint="@color/white"
    

    As I remember, this shadow is well-know problem for Floating action buttons, so please take a look at these additional libraries:

    http://android-arsenal.com/tag/173

    which may help you to replace this broken element.

    Check also:

    • Change color of Floating Action Button from Appcompat 22.2.0 programmatically
    • Change background on FloatingActionButton?

    Hope it help.

    0 讨论(0)
  • 2021-01-07 17:48

    You can remove problematic shadow by adding this attributes to your FloatingActionButton:

    app:borderWidth="0dp"
    app:elevation="6dp"
    

    There is no background color for FloatingActionButton. You change this component color by:

    app:backgroundTint="@color/YOURCOLOR"
    

    Remember to have in your parent layout following line:

    xmlns:app="http://schemas.android.com/apk/res-auto"
    
    0 讨论(0)
提交回复
热议问题