android - How to customise Floating Action Button to have different look and feel for enabled and disabled FAB state in Android?
问题描述
There is no difference in appearance when FAB is disabled or enabled. How can I achieve a different look and feel for enabled and disabled FAB via xml?
解决方案
You could change the icon when the button is enabled/disabled? For example, if your FAB has an image of a "plus", change this to a red "plus" when disabled and green when enabled.
To do this, you will need to keep track of the state of the FAB.
private var fabEnabled = true
override fun onCreate(savedInstanceState: Bundle?) {
...
fab.setOnClickListener { view ->
if (fabEnabled) {
fab.setImageResource(R.drawable.ic_green_plus)
} else {
fab.setImageResource(R.drawable.ic_red_plus)
}
}
Change fabEnabled as you wish, such as via another button.
Note this answer is in Koltin. Please tell me if you would rather it in java.
推荐阅读
- javascript - 将文本框值从一个文件传递到另一个文件
- sql - SQL 优化 - 您将如何使用一堆 AND 加快查询速度?
- reactjs - src、public 和 build 文件夹的含义
- javascript - if 语句中的速记承诺未按预期工作
- git - git 添加 . 失败,抱怨目录不存在
- reactjs - React + Redux:TypeError:无法读取未定义的属性“推送”
- amazon-web-services - AWS Codebuild 在 EB Deploy 上失败 - GIT:无法遍历提交的父项
- r - 基于内部列表元素值的列表子集列表
- android - 如何将 HTML5 网站部署为移动应用程序?
- c - 使用 getchar() 和 putchar() 进行文件复制