【问题标题】:In Android studio how to let a image in FloatingActionButton center在 Android Studio 中,如何让图像位于 FloatingActionButton 中心
【发布时间】:2021-06-04 00:05:51
【问题描述】:

    <com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/floatingActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:contentDescription="TODO"
    android:focusable="true"
    android:scaleType="fitCenter"
    app:backgroundTint="@color/black"
    app:fabSize="auto"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/checkBox6"
    app:maxImageSize="100dp"
    app:srcCompat="@drawable/add"
    app:tint="@color/blue" />

我想在Button中使图片居中,我尝试使用约束布局,但它在FloatingActionButton内部不起作用。 我更改了我的代码,但它不起作用。

    <com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/floatingActionButton2"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginEnd="30dp"
    android:layout_marginRight="30dp"
    android:layout_marginBottom="60dp"
    android:clickable="true"
    app:backgroundTint="#000000"
    app:fabCustomSize="50dp"
    app:elevation="0dp"
    app:borderWidth="0dp"
    app:fabSize="normal"
    app:pressedTranslationZ="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:maxImageSize="100dp"
    app:srcCompat="@drawable/add"
    app:tint="@color/blue" />

<com.google.android.material.floatingactionbutton.FloatingActionButton

【问题讨论】:

标签: android floating-action-button


【解决方案1】:
<com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            app:backgroundTint="@color/colortoolbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/fab_margin"
            app:srcCompat="@drawable/world"
            app:fabSize="mini"/>

您提供了不同的边距大小。删除它们。

【讨论】:

  • 但奇怪的是我通过将 app:fabsize 更改为“mini”来解决这个问题
【解决方案2】:

最终代码是

    <com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/floatingActionButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="32dp"
    android:layout_marginRight="32dp"
    android:layout_marginBottom="40dp"
    android:clickable="true"
    app:backgroundTint="#FFFFFF"
    app:fabSize="mini"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:maxImageSize="91dp"
    app:srcCompat="@drawable/add"
    app:tint="@color/blue" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    相关资源
    最近更新 更多