Как добавить векторный цветной значок в ExtendedFloatingActionButton

Я пытаюсь добавить значок цветного вектора в ExtendedFloatingActionButton из материала Google, но он показывает черный фон на значке.

При предварительном просмотре макета он выглядит нормально, но при запуске на мобильном устройстве рядом со значком отображается черный фон.

 <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
     android:id="@+id/floating_btn_take_test"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="bottom|center"
     android:layout_marginBottom="@dimen/dimen_20dp"
     android:text="@string/txt_take_test"
     android:textColor="@color/primaryColor"
     app:backgroundTint="@color/color_white"
     app:elevation="@dimen/dimen_10dp"
     app:icon="@drawable/ic_color_icon"
     app:iconTintMode="add"
     app:layout_anchorGravity="bottom|center"
     app:rippleColor="@color/secondaryColor"
     app:shapeAppearanceOverlay="@style/ShapeAppearance_Button" />

<style name="ShapeAppearance_Button" parent="ShapeAppearance.MaterialComponents.SmallComponent">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">@dimen/dimen_10dp</item>
    <item name="android:layout_marginBottom">@dimen/dimen_30dp</item>
</style>

Как убрать черный фон вокруг иконки?

2 ответа

Решение

Если я правильно понял, вы пытаетесь добавить в FAB иконку разного цвета?

При этом не забудьте добавить app:tint="@null". В вашем случае xml должен выглядеть так.

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
    android:id="@+id/floating_btn_take_test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center"
    android:layout_marginBottom="@dimen/dimen_20dp"
    android:text="@string/txt_take_test"
    android:textColor="@color/primaryColor"
    app:backgroundTint="@color/color_white"
    app:elevation="@dimen/dimen_10dp"
    app:icon="@drawable/ic_color_icon"
    app:layout_anchorGravity="bottom|center"
    app:rippleColor="@color/secondaryColor"
    app:shapeAppearanceOverlay="@style/ShapeAppearance_Button"
    app:tint="@null" />

<style name="ShapeAppearance_Button" parent="ShapeAppearance.MaterialComponents.SmallComponent">
   <item name="cornerFamily">rounded</item>
   <item name="cornerSize">@dimen/dimen_10dp</item>
   <item name="android:layout_marginBottom">@dimen/dimen_30dp</item>
</style>

Пожалуйста, попробуйте использовать app:tint как показано ниже:-

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
    style="@style/Widget.MaterialComponents.FloatingActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="16dp"
    app:backgroundTint="@color/color_white"
    app:tint="@null"
    app:icon="@drawable/ic_color_icon"
/>

ИЛИ

+ Изменить app:backgroundTint="@null"

Другие вопросы по тегам