简体   繁体   English

如何更改FAB背景颜色

[英]How to change FAB background color

I am using Floating Action Button and I want to change the background color. 我正在使用浮动操作按钮,我想更改背景颜色。

Here is my code 这是我的代码

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/btnfab"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_gravity="right|center_vertical"
        android:layout_marginBottom="20dp"
        android:src="@drawable/search" />

Here is the code I am using to try and achieve this: 这是我用来尝试实现此目的的代码:

1- android:background="@color/mycolor"
2- android:backgroundTint="@color/white"

I am also getting corners on my FAB as shown in image. 如图所示,我也在我的FAB上找到了一个角落。 How should I remove those corner shadows? 我该如何删除那些角落阴影?

在此输入图像描述

You can remove problematic shadow by adding this attributes to your FloatingActionButton : 您可以通过将此属性添加到FloatingActionButton来删除有问题的阴影:

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

There is no background color for FloatingActionButton . 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"

Declare following in your app style: 在您的应用样式中声明以下内容:

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

cheers 干杯

Please update your android support and android material design libraries in build.gradle . 请在build.gradle更新你的android支持和android材料设计库。

(Add please your build.gradle file above) (请在上面添加你的build.gradle文件)

According to these sites: 根据这些网站:

and Android Developers reference you should use only: Android开发者参考你应该只使用:

   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 http://android-arsenal.com/tag/173

which may help you to replace this broken element. 这可能会帮助您替换这个破碎的元素。

Check also: 检查还:

Hope it help. 希望它有所帮助。

First create a style in your styles.xml: 首先在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: 然后将fab的主题设置为此样式:

<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 你可以在: Android:Floating Action按钮中看到完整的描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM