2016-01-05 64 views
12

我正在使用浮動操作按鈕,我想更改背景顏色。如何更改FAB背景顏色

這裏是我的代碼

<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" /> 

這裏是我使用的嘗試和實現這一目標的代碼:

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

如圖形象在我的FAB,我也越來越角落。我應該如何去除這些角落陰影?

enter image description here

+0

從'build.gradle' – piotrek1543

+0

我沒有添加任何晶圓廠相關性添加,請依賴。我只是通過創建BlankActivity來使用fab,然後將其複製並粘貼到我的項目中。它使用我的項目的主題顏色 – Ritesh

+0

檢查這篇文章有關FAB的「角落」:http://stackoverflow.com/questions/34332587/how-to-fix-shadow-on-scaled-floatingactionbutton-using-support- library-23/34345100#34345100 –

回答

18

您可以通過添加這個屬性移除有問題的陰影您FloatingActionButton

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

沒有爲FloatingActionButton無背景色。您可以通過更改此組件的顏色:

app:backgroundTint="@color/YOURCOLOR" 

請記住,在你的父佈局以下行:

xmlns:app="http://schemas.android.com/apk/res-auto" 
6

請更新您的build.gradle支持Android和Android材料設計庫。

(添加取悅你上面的的build.gradle文件)

根據這些站點:

Android Developers reference你應該只使用:

android:backgroundTint="@color/white" 

我記得,這個影子是浮動的操作按鈕衆所周知的問題,所以請看看這些額外的庫:

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

它可以幫助你更換此元件破裂。

檢查也:

希望它能幫助。在您的應用風格

7

申報如下:

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

歡呼

+0

雖然這會起作用,但它不僅會影響FAB。所以我會推薦一個不同的解決方案。 – tir38

+0

雖然這可能對每個人都不是一個好的解決方案(如果他們希望他們的colorAccent與FAB不同),對我來說這是一個完美的解決方案。 –