简体   繁体   English

如何自定义浮动操作按钮形状?

[英]How do I customize a Floating Action Button shape?

I am trying to create a custom FAB with a custom shape.我正在尝试使用自定义形状创建自定义 FAB。 I have tried setting the android: background along with the shapeAppearanceOverlay and shapeAppearance property and still no luck.我已经尝试设置android: background以及shapeAppearanceOverlayshapeAppearance属性,但仍然没有运气。

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/edit_score_fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:shapeAppearance="@drawable/edit_score_fab"
            app:shapeAppearanceOverlay="@drawable/edit_score_fab"
            android:background="@drawable/edit_score_fab"/>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <size
        android:height="5dp"
        android:width="10dp"/>
    <corners android:bottomLeftRadius="4dp" />
    <solid android:color="@color/color_primary"/>
</shape>

My result我的结果

在此处输入图片说明

What I want我想要的是

在此处输入图片说明

You can use the ExtendedFloatingActionButton with a custom shape appearance.您可以使用具有自定义形状外观的ExtendedFloatingActionButton
Something like:就像是:

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

with:和:

  <style name="ShapeAppearanceOverlayExtended" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">0dp</item>
    <item name="cornerSizeTopLeft">50%</item>
  </style>

在此处输入图片说明

if you want an expandable fully customizable FAB :如果你想要一个可扩展的完全可定制的 FAB :

First download my library Viper Pack and install it in your project.首先下载我的库Viper Pack并将其安装在您的项目中。

Second, go to your activity onCreate cycle and add it using Lava.app.addLavaFab(...) methods from Lava class :其次,转到您的活动onCreate循环并使用 Lava 类中的Lava.app.addLavaFab(...)方法添加它:

Simple Lava Fab :简单的熔岩工厂:

Lava.app.addLavaFab(Context c, int imgres, String text, View.OnClickListener lis) lets you to add a simple image and text lava fab in your app, imgres is the fab image from resources and text is the fab text. Lava.app.addLavaFab(Context c, int imgres, String text, View.OnClickListener lis)允许您在应用程序中添加一个简单的图像和文本 lava fab,imgres 是来自资源的 fab 图像,而 text 是 fab 文本。

Lava.app.addLavaFab(Context c, int imgres, String text, String gravity, View.OnClickListener lis) lets you to add a simple image and text lava fab in your app, gravity is the fab position, can be Lava.FabPosition.LEFT or Lava.FabPosition.RIGHT or Lava.FabPosition.CENTER, imgres is the fab image from resources and text is the fab text. Lava.app.addLavaFab(Context c, int imgres, String text, String gravity, View.OnClickListener lis)让你在你的应用程序中添加一个简单的图像和文本熔岩晶圆厂,重力是晶圆厂的位置,可以是Lava.FabPosition。 LEFT 或 Lava.FabPosition.RIGHT 或 Lava.FabPosition.CENTER,imgres 是来自资源的 fab 图像,而 text 是 fab 文本。

Lava.app.addLavaFab(Context c, Drawable d, String text, View.OnClickListener lis) lets you to add a simple image and text lava fab in your app, d is the fab image as a drawable and text is the fab text. Lava.app.addLavaFab(Context c, Drawable d, String text, View.OnClickListener lis)允许您在应用程序中添加简单的图像和文本 lava fab,d 是作为 drawable 的 fab 图像,text 是 fab 文本。

Lava.app.addLavaFab(Context c, Drawable d, String text, String gravity, View.OnClickListener lis) lets you to add a simple image and text lava fab in your app, gravity is the fab position, its can be Lava.FabPosition.LEFT or Lava.FabPosition.RIGHT or Lava.FabPosition.CENTER , d is the fab image as a drawable and text is the fab text. Lava.app.addLavaFab(Context c, Drawable d, String text, String gravity, View.OnClickListener lis)让你在你的应用程序中添加一个简单的图像和文本熔岩晶圆厂,重力是晶圆厂的位置,它可以是Lava.FabPosition.LEFTLava.FabPosition.RIGHTLava.FabPosition.CENTER , d 是可绘制的 fab 图像, text 是 fab 文本。

You can get simple lava fab textview and imageview using Lava.app.fabtext and Lava.app.fabimage .您可以使用Lava.app.fabtextLava.app.fabimage获得简单的 lava fab textview 和 imageview。

Custom Lava Fab :定制熔岩工厂:

Lava.app.addLavaFab(Context c, View fixedview, View hiddenview, View.OnClickListener lis) lets you to add a custom lava fab in your app, fixedview is the always-appear view in fab, its can be an image, lottie animation, anything; Lava.app.addLavaFab(Context c, View fixedview, View hiddenview, View.OnClickListener lis)允许你在你的应用程序中添加一个自定义的熔岩晶圆厂,fixedview 是晶圆厂中始终出现的视图,它可以是一个图像,lottie 动画, 任何事物; hiddenview is the hidden view that is only appears when user press the fab, its can be a text or anything else. hiddenview 是只有在用户按下 fab 时才会出现的隐藏视图,它可以是文本或其他任何内容。

Lava.app.addLavaFab(Context c, View fixedview, View hiddenview, String gravity, View.OnClickListener lis) lets you to add a custom lava fab in your app, fixedview is the always-appear view in fab, its can be an image, lottie animation, anything; Lava.app.addLavaFab(Context c, View fixedview, View hiddenview, String gravity, View.OnClickListener lis)允许你在你的应用中添加一个自定义的熔岩晶圆厂,fixedview 是晶圆厂中始终出现的视图,它可以是一个图像,乐天动画,任何东西; hiddenview is the hidden view that is only appears when user press the fab, its can be a text or anything else, gravity is the fab position, its can be Lava.FabPosition.LEFT or Lava.FabPosition.RIGHT or Lava.FabPosition.CENTER . hiddenview 是只有当用户按下 fab 时才会出现的隐藏视图,它可以是文本或其他任何东西,gravity 是 fab 位置,它可以是Lava.FabPosition.LEFTLava.FabPosition.RIGHTLava.FabPosition.CENTER .

You can get the lava fab itself as a LinearLayout by Lava.app.fab .您可以通过Lava.app.fab将熔岩工厂本身作为 LinearLayout Lava.app.fab

Very easy to add, give it a try ans share it to others :)很容易添加,试一试并分享给其他人:)

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

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