android - 我可以在材质芯片上创建渐变背景颜色吗?
问题描述
我相信答案是肯定的,因为我可以在这里看到 react 的答案,但我不知道如何在 Kotlin/Java 中做到这一点。我看到的唯一选项是setChipBackgroundColorResource
或setBackgroundColor
两者都需要颜色资源。如何使用渐变作为背景?
- 我知道如何为任何常规视图设置背景,但只是芯片有问题。我已经尝试过了,
chip.setBackgroundResource(R.drawable.gradient_background)
但它只会使背景变灰
解决方案
目前你不能这样做。
- 该方法
setChipBackgroundColorResource
适用于color
非drawable
- 该方法
setChipBackgroundColor
正在使用ColorStateList
- 方法 ,只会产生警告
setBackgroundResource
:setBackgroundDrawable
setBackground
Do not set the background; Chip manages its own background drawable.
这是因为 Chip 小部件是围绕ChipDrawable
. ChipDrawable 只是一个MaterialShapeDrawable
管理它的背景的东西,比如:
* chipStartPadding iconEndPadding closeIconStartPadding chipEndPadding * + + + + * | | | | * | iconStartPadding | textStartPadding textEndPadding | closeIconEndPadding | * | + | + + | + | * | | | | | | | | * vvvvvvvv * +-----+----+------------+----+----+--------------- ------+----+----+----------+----+-----+ * | | | XX | | | XX XXX XXX | | | XX | | | * | | | XX | | | XXXXXXX | | | XX XX | | | * | | | XX XX | | | X XXXX X XXX | | | XX | | | * | | | XXX | | | XXXXXX | | | XX XX | | | * | | | X | | | XX XXXX | | | XX | | | * +-----+----+------------+----+----+--------------- ------+----+----+----------+----+-----+ * ^ ^ ^ * | | | * + + + * chipIconSize *动态* closeIconSize *
推荐阅读
- performance - 减少 Google Apps 脚本中的脚本运行时间?
- android - 重建安卓LK
- sql - 查看sql中按时间间隔分组的二次请求分布
- oracle - 我有这个小的 PL/SQL 代码,但我想修改它以包括预订开始和结束日期
- google-sheets - COUNTA(QUERY()) 没有产生预期的结果
- python - 异常不会打印出所需的输出并在 Selenium 中退出程序
- r - R set.seed 由另一台用户在另一台计算机上复制
- python - 由于 .ebextensions 文件,Elastic Beanstalk 部署失败
- eclipse - 当我试图找到它时,我的 OpenCV 库路径没有返回
- sql - postgesql如何美加入几张表