如何在android上更改浮动操作按钮(FAB)的形状?

如何在android上更改浮动操作按钮(FAB)的形状?,android,android-layout,shapes,floating-action-button,android-shapedrawable,Android,Android Layout,Shapes,Floating Action Button,Android Shapedrawable,在我们的android应用程序中,我们需要创建一个浮动动作按钮,它不是默认的圆形,而是一个带有三个圆角的正方形。该晶圆厂如下图所示: 我设法创建了这样一个表单,但不知道如何将其应用到我的晶圆厂,或者是否可能。形状的xml文件如下所示: <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:c

在我们的android应用程序中,我们需要创建一个浮动动作按钮,它不是默认的圆形,而是一个带有三个圆角的正方形。该晶圆厂如下图所示:

我设法创建了这样一个表单,但不知道如何将其应用到我的晶圆厂,或者是否可能。形状的xml文件如下所示:

   <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle">
    <solid android:color="@color/red" />
    <corners
      android:topLeftRadius="90dp"
      android:topRightRadius="90dp"
      android:bottomRightRadius="90dp"/>
   </shape>
allprojects {
   repositories {
       ...
       maven { url 'https://jitpack.io' }
   }
}
但这只会改变我按钮内的图标。 我想我需要扩展floatingAction按钮,但我不知道如何扩展


有没有办法改变晶圆厂的形状?如果有人已经做到了这一点,我希望看到一个示例。

您正在寻找的是更改按钮的背景

android:background="@drawable/my_shape"

无法扩展支持库提供的FloatingActionButton,因为需要替换的方法已设置为private。(这是支持库的官方AndroidX替代品)在10月~12月的版本上,这将使您可以正式轻松地完成这项工作(无需扩展视图)

但是现在还没有,所以在此期间,我用了robertlevonyan的叉子,经过一些轻微的修改,它允许您使用自己的自定义形状。源代码是可用的

要将它添加到Gradle的项目中,您需要使用。您可以这样添加它:

   <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle">
    <solid android:color="@color/red" />
    <corners
      android:topLeftRadius="90dp"
      android:topRightRadius="90dp"
      android:bottomRightRadius="90dp"/>
   </shape>
allprojects {
   repositories {
       ...
       maven { url 'https://jitpack.io' }
   }
}
然后实现我的fork:

implementation 'com.github.JediBurrell:customFloatingActionButton:-SNAPSHOT'
接下来我们将创建形状,您创建的形状应该可以正常工作

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <corners
        android:topLeftRadius="@dimen/fab_circle_radius"
        android:topRightRadius="@dimen/fab_circle_radius"
        android:bottomRightRadius="@dimen/fab_circle_radius" />
    <solid android:color="@color/colorAccent" />
</shape>

然后最后将其添加到布局中(Github repo中列出了更多晶圆厂选项):


下面是它的样子:


使用材质组件库,您可以使用
形状PearanceOverlay

<com.google.android.material.floatingactionbutton.FloatingActionButton
    app:shapeAppearanceOverlay="@style/fab_3_rounded"
    .../>

与:


圆的
50%
0dp

无需更新任何外部库从更新您的Gradle文件

implementation 'com.google.android.material:material:1.1.0'

这将使您能够访问

  <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


然后你可以像普通按钮一样更改其属性

你应该编辑你的答案,以格式化示例代码部分。你找到解决方案了吗?虽然我找到了另一个解决方案,但我认为@JediBurrell提供的答案是更好的方法,效果很好。实际上,正如我所读到的,现在应该可以了:。问题是,我没有看到太多关于它的示例和教程。你知道如何使用它吗?你能演示一下如何制作一些基本的东西,比如圆角水平晶圆厂(比如在联系人详细信息屏幕中)?
implementation 'com.google.android.material:material:1.3.0'
  <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />