浮动操作按钮未显示矢量资源

5
我正在尝试将一个向量图形设置到fab中,但结果是这样的:enter image description here。 您可以看到向量图形存在,但位置偏下和偏左。这是布局问题。
    <?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBg"
    tools:context=".ui.main.fragments.HomeFragment">


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_new_sale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        app:backgroundTint="@color/colorAccent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/ic_cart_plus"
        android:scaleType="center"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_home_cards"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:foregroundGravity="center_vertical|center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

这里是向量:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="200dp"
    android:height="200dp"
    android:viewportWidth="200"
    android:viewportHeight="200">
    <path
        android:fillColor="#FF000000"
        android:pathData="M165.1,119.28L75,119.28l2,9.84h82.48a7.37,7.37 0,0 1,7.19 9l-1.7,7.46a17.2,17.2 0,1 1,-19.54 3.19L81,148.77a17.21,17.21 0,1 1,-20.6 -2.63L38.85,40.62L17.38,40.62A7.38,7.38 0,0 1,10 33.24L10,28.33A7.37,7.37 0,0 1,17.38 21h31.5a7.38,7.38 0,0 1,7.23 5.9l2.81,13.77h120.7a7.37,7.37 0,0 1,7.19 9l-14.52,63.91A7.38,7.38 0,0 1,165.1 119.28ZM135.38,72.58L120.63,72.58L120.63,60.28a4.92,4.92 0,0 0,-4.92 -4.91h-4.92a4.91,4.91 0,0 0,-4.91 4.91v12.3L91.13,72.58a4.91,4.91 0,0 0,-4.92 4.91v4.92a4.92,4.92 0,0 0,4.92 4.92h14.75L105.88,99.62a4.91,4.91 0,0 0,4.91 4.91h4.92a4.92,4.92 0,0 0,4.92 -4.91L120.63,87.33h14.75a4.92,4.92 0,0 0,4.91 -4.92L140.29,77.49A4.91,4.91 0,0 0,135.38 72.58Z" />
</vector>

我很感激您能帮助解决这个问题


分享你的矢量可绘制XML! - Md Golam Rahman Tushar
尝试使用android:src="@drawable/ic_cart_plus"而不是app:srcCompat="@drawable/ic_cart_plus"。 - Nice umang
添加了矢量图。使用src而不是srcCompat没有任何变化。 - fivunlm
你可以尝试将 android:widthandroid:height 设置为 24dp,看看是否有效?浮动操作按钮在大小方面相当受限制。根据版本的不同,你会发现不同的情况。 - tynn
如果问题仍然存在,请在build.gradle中添加以下内容 android { defaultConfig { vectorDrawables.useSupportLibrary = true } } - Kasım Özdemir
任何建议都没有起作用。我仍然面临问题,但我发现它与mercadopago依赖有关。 - fivunlm
7个回答

2

设置

android {
   defaultConfig {
     vectorDrawables.useSupportLibrary = true
    }
 }

在你的build.gradle文件中


1
我认为你需要更新你的依赖项。
在使用这些依赖项时,我已经让它正常工作了。
implementation 'androidx.appcompat:appcompat:1.2.0-alpha02'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation "com.google.android.material:material:1.1.0"


<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/drawable_test"
    />

Screen shot


1
我知道这是一个老问题,可能还有其他人遇到同样的问题,所以我添加了这个答案。正如其他人建议的那样,将以下依赖项添加到build.gradle中:
implementation 'androidx.appcompat:appcompat'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'

正如其他人建议的那样,在build.gradle中添加以下内容:

android {
   defaultConfig {
     vectorDrawables.useSupportLibrary = true
    }
 }

在矢量图中,将android:widthandroid:height改为android:width="24dp"android:height="24dp"
在布局中的com.google.android.material.floatingactionbutton.FloatingActionButton中,添加app:fabCustomSize="56dp",或者替换为app:fabSize="normal"

0

你可以用这个想法解决你的问题,

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab_new_sale"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="16dp"
    app:backgroundTint="@color/colorAccent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:src="@drawable/ic_cart_plus"
    android:scaleType="center"/>

在这里,您可以使用android:src而不是app:srcCompat


0

我使用以下依赖项成功运行了它

implementation 'androidx.appcompat:appcompat'
implementation 'com.google.android.material:material:1.1.0'
implementation "androidx.constraintlayout:constraintlayout:2.0.0-beta4"

尝试更新这些依赖项。我不需要进行任何重新缩放。也许还可以参考thisthis的问题,因为可能是您的可绘制对象没有正确居中,导致圆形出现问题。


0

你只需要将srcCompat更改为src,这样就可以解决你的问题了。我附上了我的FAB代码以方便你:

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_explore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/dp_16"
        android:src="@drawable/ic_explore_black_24dp"
        app:backgroundTint="@color/accent"
        app:tint="@color/color_white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

0

FloatingActionButton小部件提供了Material Design浮动操作按钮组件的完整实现。

升级您的依赖项

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

使用 AppCompat 和 app:srcCompat 是将矢量可绘图文件集成到应用程序中最简单可靠的方法。

最后

 <com.google.android.material.floatingactionbutton.FloatingActionButton
      app:srcCompat="@drawable/your_vector"

请注意 - 请减小宽度,高度的值。
   <vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="20dp"
    android:height="20dp"
    android:viewportWidth="200"
    android:viewportHeight="200">

Android Studio 1.4引入了有限的向量图兼容性支持,通过在构建时生成PNG文件。然而,向量图和动画向量图支持库提供了灵活性和广泛的兼容性 - 它是一个支持库,因此您可以将其与所有Android平台版本一起使用,包括Android 2.1(API级别7+)。要配置您的应用程序以使用矢量支持库,请在应用程序模块的build.gradle文件中添加vectorDrawables元素。

请使用以下代码片段来配置vectorDrawables元素:

//For Gradle Plugin 2.0+
 android {
   defaultConfig {
     vectorDrawables.useSupportLibrary = true
    }
 }

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