如何为 android.support.design 中的 FloatingActionButton 设置样式

How to style the FloatingActionButton from the android.support.design

我正在尝试为 android.support.design 的 FloatingActionButton 添加更明显的焦点效果。

到目前为止,我已经测试过向按钮添加自定义背景:

 <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_done_24dp"
            android:background="@drawable/focus_gold"
            app:backgroundTint="#F7A200"
            app:pressedTranslationZ="12dp" />

可绘制对象 focus_gold 为:

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_focused="true" android:drawable="#ff9800"/>
 </selector>

结果如下(红色背景正常):

不专心:

重点:

有没有人想用这个库在 fab 按钮上设置一个漂亮而简单的焦点效果?

谢谢,

文森特

您可以试试非官方的 FloatingActionButton 库!这是 github link: https://github.com/makovkastar/FloatingActionButton

希望对您有所帮助!

我的建议,

  1. 删除 android:background 属性。
  2. 将 fab_gold.xml 从 res/drawable/ 移动到 res/color/。
  3. 将 backgroundTint 更改为 app:backgroundTint="@color/focus_gold"

确保将 focus_gold.xml 更改为此

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="#ff9800" android:state_focused="true"/>
  <item android:color="#F7A200" />
</selector>