android:从代码动态更改FAB( float 操作按钮)图标

标签 android material-design floating-action-button

如何在运行时更改 Activity 中的 FAB 图标。我有这个代码->

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fabMainActivity);

我知道使用 fab.setBackgroundDrawable(); 可以做到这一点,但我是 android 新手,不明白如何做到这一点。

我们将不胜感激。

谢谢

最佳答案

更改 FloatingActionButton 来源:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            floatingActionButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_full_sad, context.getTheme()));
        } else {
            floatingActionButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_full_sad));
    }

这可以替换为支持库中的以下代码:

floatingActionButton.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.ic_full_sad));

关于android:从代码动态更改FAB( float 操作按钮)图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33140706/

相关文章:

android - 过滤仅适用于我的应用程序的 SMS 消息

android - 如何在单个 AlertDialog 上添加多个按钮

flutter - 当我滚动时,ListView 会上下移动 - flutter

android - Material Design 排版和 Snackbars

android - 如何将可滚动选项卡与 Android 中的键线对齐

android - float 操作按钮下的 Admob

android - setImageResource(int) 在 setBackgroundTintColorList(ColorStateList) FloatingActionButton 之后不起作用

android - float 操作按钮重力不起作用

android - 我需要为使用 viewpager 和 tablayout 创建的选项卡分配一个唯一的选项卡 ID?

android - 将 View 放置在 android 中 CoordinatorLayout 中的另一个 View 下方