如何在扩展浮动操作按钮中居中对齐文本?

How to center align text in Extended Floating Action Button?

我想将扩展浮动操作按钮中的文本居中对齐,但我做不到。正如您在下图中看到的,文本未居中对齐。

这是扩展浮动操作按钮的代码:

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:id="@+id/compress_start_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="S"
        app:backgroundTint="?android:colorAccent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:textColor="@color/textColorPrimaryDark"
        app:layout_constraintStart_toStartOf="parent"
        android:textAlignment="center"
        />

我做错了什么吗?任何帮助将不胜感激。

请在您的 ExtendedFloatingActionButton 中添加这两行

 android:paddingStart="0dp"
 android:paddingEnd="0dp

这样使用

  <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:gravity="center"
    android:paddingStart="0dp"
    android:paddingEnd="0dp"
    android:text="@string/ic_lin_camera" />

仅对文本使用 ExtendedFloatingActionButton 时,在您的布局 xml 中您可以使用

style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"

或通过

扩展上述样式
<style name="extended_fab_text_only" parent="Widget.MaterialComponents.ExtendedFloatingActionButton">
    <item name="android:textAppearance">@style/myTextAppearance</item>
</style>

而你,在你的布局中作为你工厂的风格xml

style="@style/extended_fab_text_only"

文档:https://material.io/components/buttons-floating-action-button/android#extended-fabs 在扩展 FAB > 样式下:

Text-only when extended style Widget.MaterialComponents.ExtendedFloatingActionButton