Setting src and background for FloatingActionButton

会有一股神秘感。 提交于 2019-11-30 05:01:34

问题


When I use background and src in android.support.design.FloatingActionbutton it is not set correctly. Instead it is displayed as

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pink"
android:src="@drawable/ic_action_barcode_2"
android:layout_gravity="bottom|right"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp" />

but when I use ImageView it appears correctly as

<ImageView
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pink"
android:src="@drawable/ic_action_barcode_2"
android:layout_gravity="bottom|right"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp" />

why is FloatingActionButton is not displayed correctly? What should I change in my code?


回答1:


Floating action button's background does not need to be changed, you just apply a tint and then add your icon as usual

<android.support.design.widget.FloatingActionButton
    ...
    app:backgroundTint="@color/ic_action_barcode_2"
    android:src="@drawable/ic_add" />

This provides you with a round button still but in the colour you desire. In this case the app namespace is used for the support library features:

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




回答2:


i faced the similar problem. I tried to set src or background of FloatingActionButton, but couldn't fill the button by src. following code solved problem for me.

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    .
    .
    .
    android:background="@drawable/round_icon"
    android:backgroundTintMode="src_over">


来源:https://stackoverflow.com/questions/32036656/setting-src-and-background-for-floatingactionbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!