简体   繁体   English

如何将 FAB 塑造为圆角按钮(药丸形状),类似于联系人应用程序的联系人详细信息屏幕?

[英]How to shape a FAB to be rounded corners button (pill shape), similar to in contact-details screen of Contacts app?

Background背景

Initially, a FAB (floating action button) only had one shape : circle, and it only had the option to have an icon in it.最初,FAB(浮动操作按钮)只有一个形状:圆形,并且它只能选择在其中包含一个图标。

The problem问题

In the past year or so, Google added various screens in its apps, that show a FAB that changes its size.在过去一年左右的时间里,谷歌在其应用程序中添加了各种屏幕,显示改变其大小的 FAB。 For example, if you open its Contacts app and choose a contact, you a FAB as such:例如,如果你打开它的通讯录应用程序并选择一个联系人,那么你就是一个 FAB:

在此处输入图片说明

And if you scroll a bit, it gets to be a circle, with just the icon in it:如果你滚动一点,它就会变成一个圆圈,里面只有一个图标:

在此处输入图片说明

I want to know how to make such a FAB, and also use similar one with just an icon in the middle (without text), meaning a rounded corners one.我想知道如何制作这样的 FAB,并使用类似的,中间只有一个图标(没有文字),意思是圆角。

Something like that:类似的东西:

在此处输入图片说明

What I've tried and found我的尝试和发现

At first, I tried to just set the width of the FAB to be larger, but this didn't help, as it insisted on being a perfect circle (fit in a square, and without edges).起初,我试图将 FAB 的宽度设置得更大,但这并没有帮助,因为它坚持是一个完美的圆形(适合正方形,并且没有边缘)。

Seeing that maybe FAB can't handle this currently, I used MaterialCardViewinstead:看到可能 FAB 目前无法处理这个,我改用 MaterialCardView:

                <com.google.android.material.card.MaterialCardView

android:layout_width="60dp" android:layout_height="32dp" android:clickable="true" android:focusable="true" app:cardBackgroundColor="..." app:cardCornerRadius="16dp" > android:layout_width="60dp" android:layout_height="32dp" android:clickable="true" android:focusable="true" app:cardBackgroundColor="..." app:cardCornerRadius="16dp">

                    <androidx.appcompat.widget.AppCompatImageView
                        android:layout_width="wrap_content" android:layout_height="wrap_content"
                        android:layout_gravity="center" app:srcCompat="..."/>
                </com.google.android.material.card.MaterialCardView>

This works, but the clicking effect is gone.这有效,但点击效果消失了。

I also tried MaterialButton, but this doesn't support an icon in the middle, only on the various of the text inside.我也试过 MaterialButton,但这不支持中间的图标,只支持里面的各种文字。

Later, I've noticed that FAB actually does have a way to customize the shape of itself :后来,我注意到 FAB 实际上确实有一种方法可以自定义自身的形状:

https://developer.android.com/reference/com/google/android/material/floatingactionbutton/FloatingActionButton#setshapeappearance https://developer.android.com/reference/com/google/android/material/floatingactionbutton/FloatingActionButton#setshapeappearance

https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/floatingactionbutton/FloatingActionButton.java https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/floatingactionbutton/FloatingActionButton.java

Also, as for the expanding FAB, I've found this:另外,对于不断扩大的 FAB,我发现了这一点:

https://developer.android.com/reference/com/google/android/material/floatingactionbutton/ExtendedFloatingActionButton https://developer.android.com/reference/com/google/android/material/floatingactionbutton/ExtendedFloatingActionButton

https://material.io/develop/android/components/extended-floating-action-button/ https://material.io/develop/android/components/extended-floating-action-button/

So looking at its code, I've found that it uses a shape of a pill, and I tried to do a similar thing to the normal FAB :所以查看它的代码,我发现它使用了一个药丸的形状,我试图做一个与普通 FAB 类似的事情:

class PillFab @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    FloatingActionButton(context, attrs, defStyleAttr) {
    init {
        val shapeAppearanceModel =
            ShapeAppearanceModel(context, attrs, defStyleAttr, DEF_STYLE_RES, ShapeAppearanceModel.PILL)
        shapeAppearance = shapeAppearanceModel
    }

    companion object {
        private val DEF_STYLE_RES = R.style.Widget_MaterialComponents_FloatingActionButton
    }
}

Sadly, this didn't work at all.可悲的是,这根本不起作用。 It's still in a circle shape.它仍然是圆形的。

The question问题

How can I set a FAB (with clicking effect) that has rounded corners and a single icon inside?如何设置带有圆角和单个图标的 FAB(具有点击效果)?

It is not the perfect answer because you are looking for an ExtendedFAB without text.这不是完美的答案,因为您正在寻找没有文本的 ExtendedFAB。

However you can obtain something similar using a MaterialButton changing the component's shape using the attribute shapeAppearanceOverlay .但是,您可以使用MaterialButton获得类似的东西,使用属性shapeAppearanceOverlay更改组件的形状。

在此处输入图片说明

<com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MyApp.Button.Rounded"
            app:icon="@drawable/outline_cached_black_18dp"
            app:iconPadding="0dp"
            app:iconGravity="textStart"
            />

using:使用:

  <style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
  </style>

Currenty the app:iconGravity doesn't support the center value.当前app:iconGravity不支持center值。 A workaround to center the icon is using these attrs:使图标居中的解决方法是使用这些属性:

            app:iconPadding="0dp"
            app:iconGravity="textStart"

If you would like to override some theme attributes from a default style now you can use new materialThemeOverlay attribute.如果您现在想从默认样式覆盖某些主题属性,您可以使用新的materialThemeOverlay属性。

Something like:就像是:

<style name="MtButtonStyle"
 parent="Widget.MaterialComponents.Button">
   <item name=“materialThemeOverlay”>@style/MyButtonThemeOverlay</item>
</style>

<style name="MyButtonThemeOverlay">
  <item name="colorPrimary">@color/...</item>
  <item name="colorOnSurface">@color/....</item>
</style>

You can use ExtendedFloatingActionButton 您可以使用ExtendedFloatingActionButton

for more information check 1.1.0-alpha06 Try this 有关更多信息,请查看1.1.0-alpha06试试这个

Use below dependencies 使用以下依赖项

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

SAMPLE CODE 示例代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:gravity="center"
    android:orientation="vertical">

    <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nilu Pilu"
        app:icon="@drawable/ic_search_black" />

</LinearLayout>

OUTPUT OUTPUT

在此输入图像描述

您可以使用CoordinatorLayout.Behavior在 Recyclerview 的滚动事件上将形状、效果应用于 android 中的任何视图。

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

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