2016-06-18 107 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> 
相关问题