android:从代码中动态更改FAB(浮动操作按钮)图标 [英] android: dynamically change FAB(Floating Action Button) icon from code

查看:576
本文介绍了android:从代码中动态更改FAB(浮动操作按钮)图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在运行时更改活动中的FAB图标.我有此代码->

How to change FAB icon in an Activity during runtime. I have this code ->

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

我知道使用fab.setBackgroundDrawable();是可能的,但是我是android的新手,不知道该怎么做.

I know this is possible using fab.setBackgroundDrawable(); but i am a newbie to android, don't understand how to do this.

任何帮助将不胜感激.

谢谢

推荐答案

更改FloatingActionButton源:

Changing FloatingActionButton source:

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));
    }

可以用以下支持库中的代码代替:

This can be replaced by following code from the support library instead:

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

这篇关于android:从代码中动态更改FAB(浮动操作按钮)图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆