首页 > 解决方案 > 我可以在材质芯片上创建渐变背景颜色吗?

问题描述

我相信答案是肯定的,因为我可以在这里看到 react 的答案,但我不知道如何在 Kotlin/Java 中做到这一点。我看到的唯一选项是setChipBackgroundColorResourcesetBackgroundColor两者都需要颜色资源。如何使用渐变作为背景?

标签: androidkotlinmaterial-componentsmaterial-components-androidandroid-chips

解决方案


目前你不能这样做

  • 该方法setChipBackgroundColorResource适用于colordrawable
  • 该方法setChipBackgroundColor正在使用ColorStateList
  • 方法 ,只会产生警告setBackgroundResourcesetBackgroundDrawablesetBackgroundDo 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
 *

推荐阅读