简体   繁体   中英

Can an image be used in FloatingActionButton?

I want to use an image as background of a FloatingActionButton .

I know that the default background color which is colorAccent and we can change it easily. But what about the image as its background ?

If I can then how?

Pictorial explanation will always be appreciated.

Just use like this

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="@android:color/transparent"
    android:src="@drawable/icon" />

In xml you can set like this:

<android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:layout_margin="16dp"
            android:src="@drawable/ic_add_black"
            app:borderWidth="0dp"
            app:fabSize="mini" />

and in java file you can set like this:

  ImageView iconFAB = new ImageView(this);
        iconFAB.setImageResource(R.drawable.ic_action_new);

        //set the appropriate background for the main floating action button along with its icon
        mFAB = new FloatingActionButton.Builder(this)
                .setContentView(iconFAB)
                .setBackgroundDrawable(R.drawable.selector_button_pink)
                .build();

here is selector_button_pink.xml file code

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_action_red_touch" android:state_pressed="true"></item>
    <item android:drawable="@drawable/button_action_red" android:state_pressed="false"></item>
</selector>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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