【问题标题】:How to change the background image of Floating Action Button programatically in Kotlin?如何在 Kotlin 中以编程方式更改浮动操作按钮的背景图像?
【发布时间】:2021-12-28 19:14:36
【问题描述】:

我使用以下FloatingActionButton,并使用xml 设置了背景图像。如何在 Kotlin 中以编程方式更改图像?我想用 'image-bw' 替换图像 'image-color'。

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/image_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/image-color"
    android:scaleType="center"
    app:maxImageSize="56dp"
    app:tint="@null"
    app:layout_anchor="@id/bottom_appbar"
    android:contentDescription="Categories" />

【问题讨论】:

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


    【解决方案1】:

    您可以使用setImageDrawable 函数。

    imageFab.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.your_drawable))
    

    【讨论】:

    • IIRC 你也可以使用imageFab.setImageResource(R.drawable.your_drawable)
    猜你喜欢
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 2013-09-12
    • 2019-06-11
    相关资源
    最近更新 更多