ExtendedFloatingActionButton - 如何在按钮展开时达到与收缩时相同的高度

ExtendedFloatingActionButton - how to achieve same height when button is extended as when it's shrank

我正在使用最新的 material 组件 - 1.1.0-beta01。

当 ExtendedFloatingActionButton 扩展时,它的高度也会降低。

xml 布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/main_view"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">

   <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
       android:id="@+id/fab"
       android:layout_width="wrap_content"
       android:layout_height="56dp"
       android:text="Text"
       android:textColor="#02220D"
       android:backgroundTint="#F2C203"
       style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
       android:textSize="15sp"
       android:fontFamily="sans-serif-medium"
       android:textStyle="normal"
       android:layout_marginBottom="16dp"
       android:layout_marginEnd="16dp"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintEnd_toEndOf="parent"
       app:iconTint="#02220D"
       app:icon="@drawable/ic_camera"
       app:iconPadding="10dp"
       android:insetLeft="0dp"
       android:insetBottom="0dp"
       android:insetTop="0dp"
       android:insetRight="0dp"
       />

</androidx.constraintlayout.widget.ConstraintLayout>

我尝试设置最小和最大高度,但没有成功。知道如何在按钮展开和收缩时达到相同的高度吗?

谢谢。

ExtendedFloatingActionButton是一个MaterialButton
您可以使用 android:minHeight 属性:

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
    android:minHeight="56dp"
    ../>

否则您可以定义自定义样式:

 <style name="CustomExtendedFloating"  parent="Widget.MaterialComponents.ExtendedFloatingActionButton.Icon">
    <item name="android:minHeight">56dp</item>
 </style>

值得注意的是,根据FAB和Extended FAB的规范,在展开状态下,它并没有那么高。 https://material.io/components/buttons-floating-action-button/#specs

使用 material 库提供的值代替硬编码值:

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:minHeight="@dimen/design_fab_size_normal"

尝试将 app:collapsedSize 属性 设置为您需要的尺寸

app:collapsedSize="56dp"