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

7

我想要将扩展浮动操作按钮中的文本居中对齐,但我无法做到。如下图所示,文本没有居中对齐。

enter image description here

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

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

我有什么做错了吗?非常感谢您的帮助。

3个回答

8
请在您的 ExtendedFloatingActionButton 中添加以下两行代码。
 android:paddingStart="0dp"
 android:paddingEnd="0dp

还需要 android:gravity="center" - undefined

3

使用方法如下

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

0

当您在布局XML中仅使用ExtendedFloatingActionButton进行文本时,可以使用以下代码:

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

或通过扩展上述样式来实现

<style name="extended_fab_text_only" parent="Widget.MaterialComponents.ExtendedFloatingActionButton">
    <item name="android:textAppearance">@style/myTextAppearance</item>
</style>

而你可以在你的布局xml中使用你的fab的样式

style="@style/extended_fab_text_only"

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

仅文字扩展 样式 Widget.MaterialComponents.ExtendedFloatingActionButton


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接