1

This is what my floating action button is showing爲什麼我的浮動操作按鈕不能正確顯示其圖像?

寧可只是顯示圖像的實際圓形部分,它會嘗試使我的.png文件的背景適合浮動動作按鈕。

這裏是我的XML代碼:

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/addToKitchenButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="16dp" 
    android:layout_gravity="bottom|end" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentEnd="true" 
    android:src="@drawable/fab_icon" 
    /> 

how do I get it to look like this?

我已經給我的PNG文件透明背景,所以我不知道還有什麼我需要做的。請幫忙。

謝謝。

回答

3

(假設您使用的是Android Studio) - 不是使用您自己的自定義.png作爲FloatingActionButton的src,而是使用該圖標創建一個新的矢量素材。在Android Studio中右鍵單擊您的res/drawable文件夾,然後單擊新建 - >矢量資產。

http://i.imgur.com/qThPorK.png

您使用的是內置於Android的工作室,雖然你可以導入自己的矢量資源,如果你正在做一些更多的自定義圖標。它生成XML將看起來像這樣:

<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:width="24dp" 
    android:height="24dp" 
    android:viewportWidth="24.0" 
    android:viewportHeight="24.0"> 
<path 
    android:fillColor="#FF000000" 
    android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/> 

,你可以更改填充顏色的顏色。在android:src中將你的FloatingActionButton設置爲這個可繪製的,你很好走。

+0

謝謝,這正是我所需要的,但現在我有這個問題http://stackoverflow.com/questions/39737699/why-is-my-floating-action-button-not-displaying-properly-different-問題從m它不能正常顯示 –

0

你不必提供圓角圖像。 我使用this image和我我的XML看起來像這樣

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/addFolder" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="16dp" 
    android:clickable="true" 
    android:src="@drawable/plus" 
    app:layout_anchor="@id/mainRl" 
    android:background="#ff0000" 
    app:layout_anchorGravity="bottom|right|end" 
    android:onClick="onAddFolderClick" 

    /> 

我使用#FF0000(紅色)作爲背景顏色,你用你想要的粉紅色。這對我來說可以。

0

爲了保持一致的外觀,您需要做兩件事。 這個回答假設你真的想在鏈接的外觀由您提供的是this

  1. 第一步 - 的背景顏色是什麼,但你的應用程序的口音顏色,你不需要提供任何特定的顏色。所以首先確保你的應用主題設置了重音顏色。

  2. 第二步 - 完成之後,您需要使用下面的xml代碼。 如果你想要看鏈接,你甚至不需要創建一個新的PNG。您可以從this official google material icons link輕鬆下載白色添加圖標。您可以通過谷歌使用這個鏈接的大多數標準材料圖標。


<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="@drawable/ic_add_white_24dp"/> 

希望這有助於。請讓我知道它是否改變了你的事情。

編輯::如果你想改變圖標的​​顏色和背景的東西比你的默認口音的顏色完全不同,那麼你必須要使用這些屬性 -

//自定義背景

app:backgroundTint="@color/yourCustomColor" 

//自定義圖標的顏色

android:tint="@color/yourCustomColor"  
+0

謝謝你這個工作,但現在我有這個問題http://stackoverflow.com/questions/39737699/why-is-my-floating-action-button-not-displaying-properly不同的問題從m它不會正確顯示 –