繁体   English   中英

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

[英]How to change the shape of Floating Action Button (FAB) on android?

在我们的 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>

我试图改变形式

android:src="@drawable/my_shape"

但这只会改变我的按钮内的图标。 我想我需要扩展 FloatingActionButton 但我不知道如何扩展。

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

支持库提供的 FloatingActionButton 不能扩展,因为需要替换的方法被设置为私有。 Material Components (它是支持库的官方 AndroidX 替代品)在 ~October-December 的路线图上具有Shape Theming ,这将使您能够正式且轻松地做到这一点(无需扩展视图)。

但它目前还不能用,所以在此期间,我分叉了robertlevonyan的 customFloatingActionButton,并稍作修改,它允许您使用自己的自定义形状。 源代码可在此处获得

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

allprojects {
   repositories {
       ...
       maven { url 'https://jitpack.io' }
   }
}

然后实现我的叉子:

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>

然后最后将其添加到您的布局中(更多 FAB 选项在 Github 存储库中列出):

<com.jediburrell.customfab.FloatingActionButton
    android:id="@+id/floating_action_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:layout_margin="16dp"
    app:fabType="custom"
    app:fabShape="@drawable/fab_teardrop"
    app:fabIcon="@drawable/ic_add_24dp"
    app:fabColor="@color/red" />

这是它的外观:

使用材料组件库,您可以使用shapeAppearanceOverlay

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

与:

<style name="fab_3_rounded">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
    <item name="cornerSizeBottomLeft">0dp</item>
</style>

在此处输入图片说明

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

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

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

然后你可以像普通按钮一样改变它的属性

在你的问题中,你说你试过调整android:src ,这显然是错误的方法,因为那个是关于FloatingActionButton的内容。

android:background参数将改变你的形状。 请注意它应该是圆的

我认为这个问题只是不重复,因为其他问题只涉及颜色。

您需要在XML文件中使用它:

android:background="@drawable/my_shape"

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

android:background="@drawable/my_shape"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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