2016-06-18 50 views
3

我想使用圖像作爲FloatingActionButton的背景。可以在FloatingActionButton中使用圖像嗎?

我知道默認背景顏色是colorAccent,我們可以很容易地改變它。但是這個圖像作爲背景呢?

如果我可以那麼怎麼樣?

圖片說明將不勝感激。

+2

是的,它可以。這已經問過,答案是[這裏](http://stackoverflow.com/questions/32036656/setting-src-and-background-for-floatingactionbutton) –

回答

4

只要使用類似這樣的

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

在XML中,你可以這樣設置:

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

,並在Java文件,你可以這樣設置:

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(); 

這裏是selector_button_pink .xml文件代碼

<?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> 
相關問題