【问题标题】:Floating action button not showing vector asset浮动操作按钮不显示矢量资产
【发布时间】:2020-06-11 01:03:00
【问题描述】:

我正在尝试将矢量资产设置为晶圆厂。但结果是这样的: 如您所见,矢量在那里,但在下方和左侧。这是布局

    <?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!
  • 试试 android:src="@drawable/ic_cart_plus" 而不是 app:srcCompat="@drawable/ic_cart_plus"
  • 添加了矢量绘图。使用 src 代替 srcCompat 没有改变
  • 您可以尝试将android:widthandroid:height 设置为24dp 看看是否有效?浮动操作按钮在尺寸方面非常严格。根据版本你会发现不同的一次。
  • 如果问题仍然存在,请将其添加到 build.gradle android { defaultConfig { vectorDrawables.useSupportLibrary = true } }

标签: android floating-action-button


【解决方案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"
    />

【讨论】:

    【解决方案2】:

    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"
    

    注意 - 请减少 width,height 值。

       <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 级以上)。要将您的应用配置为使用矢量支持库,请添加 将 vectorDrawables 元素添加到应用程序中的 build.gradle 文件中 模块。

    使用以下代码sn-p配置vectorDrawables元素:

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

    【讨论】:

      【解决方案3】:

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

      <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 insted of app:srcCompat

      【讨论】:

        【解决方案4】:

        使用以下依赖项对我有用

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

        尝试更新这些依赖项。我不必做任何重新调整。也许也可以参考thisthis 问题,因为这可能是您的drawable 没有正确居中并离开圆圈

        【讨论】:

          【解决方案5】:

          您只需将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" />
          

          【讨论】:

            【解决方案6】:

            设置

            android {
               defaultConfig {
                 vectorDrawables.useSupportLibrary = true
                }
             }
            

            在你的build.gradle

            【讨论】:

              【解决方案7】:

              我知道这是老问题,其他人可能有同样的问题,所以我添加了这个答案。

              按照其他人的建议,将以下依赖项添加到 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" 来代替它。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2016-03-03
                • 2016-11-09
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2022-06-25
                相关资源
                最近更新 更多