【问题标题】:Floating Action Button is not showing an image in Android?浮动操作按钮在 Android 中不显示图像?
【发布时间】:2020-07-17 17:00:45
【问题描述】:

正如标题所说,我不知道如何解决这个问题。我读了这个 problem 但那是在 2018 年,我对所有这些库和依赖项都不是很熟悉它们是如何工作的。但是从我声明的依赖项来看,从 Android Studio 来看,这就是我得到的:

我想在 FAB 中添加某种“附加文档”图标/图像。我试图通过添加新矢量资产来做到这一点,但图像没有出现在我的可绘制文件夹中。如何找到一些不错的图标/图像并将其设置为 FAB?

这里是 XML:

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:icon="@drawable/attache_file">

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

【问题讨论】:

  • 在 android:icon 上说 android:src

标签: android xml floating-action-button material-components-android


【解决方案1】:

根据documentation,您必须使用android:src="@drawable/attache_file"

【讨论】:

  • 该死的,我想念那个,3小时就浪费了:) 谢谢,它有效
【解决方案2】:

要使用com.google.android.material.floatingactionbutton.FloatingActionButton,请添加Material Components library

implementation 'com.google.android.material:material:1.1.0'

然后使用app:srcCompat属性:

  <com.google.android.material.floatingactionbutton.FloatingActionButton
      style="@style/Widget.MaterialComponents.FloatingActionButton"
      app:srcCompat="@drawable/..."
      .../>

【讨论】:

    【解决方案3】:

    我找到了解决方案,只需在 dimens.xml 中添加以下语句

    &lt;dimen name="design_fab_image_size" tools:override="true"&gt;56dp&lt;/dimen&gt;

    并且必须需要在您的 FAB 设计 xml 中添加 app:tint="@null"

    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_anchor="@id/bottomBar"
            app:srcCompat="@drawable/ic_logo"
            app:tint="@null" />```
    
    Happy to help :) 
    

    【讨论】:

      猜你喜欢
      • 2021-10-24
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 2019-04-05
      • 2015-08-07
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多