Material 组件芯片颜色样式
Material Components Chip colour styling
在我的 XML 中,我只是声明一个 ChipGroup
如下:
<com.google.android.material.chip.ChipGroup
android:id="@+id/chipGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
然后动态添加每个 Chip
:
ChipGroup chipGroup = findViewById(R.id.chipGroup);
for (String name : names) {
Chip chip = new Chip(activity);
chip.setText(name);
chipGroup.addView(chip);
}
所选筹码的颜色(下面的浅绿色)似乎来自 colorSecondaryContainer
属性(我知道这一点,因为当我在我的主题中更改它时,筹码颜色会改变)。我宁愿它来自 colorPrimaryContainer
,但按原样也可以,只是文本颜色不适合 colorSecondaryContainer
...特别是,文本颜色 不适合 似乎如我所料来自 colorOnSecondaryContainer
,因为 colorOnSecondaryContainer
在我的主题中是深色的,但我看到的是浅色芯片颜色上的浅色文字(和无论选中状态如何,文本颜色都是浅色的):
如何使我的 Chip 样式总体上符合我的 material 主题,而不必通过 chip.setTextColor()
等设置器来设置这些东西?我没有通过 XML 声明任何 Chip,所以我也无法覆盖单独声明中的样式。
在 theme.xml
文件中添加 Chip 样式
theme.xml
中的 ChipCustomStyle
<style name="ChipCustomStyle" parent="@style/Widget.MaterialComponents.Chip.Choice">
<item name="chipBackgroundColor">@color/chip_background_color</item>
<item name="android:textColor">@color/chip_text_color</item>
<item name="chipCornerRadius">5dp</item>
</style>
在 res
下创建 color
资源文件夹并添加 chip_background_color.xml
用于芯片背景颜色和 chip_text_color.xml
用于芯片文本颜色
chip_background_color.xml 在颜色文件夹中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/green" android:state_checked="true" />
<item android:color="@color/white" />
</selector>
chip_text_color.xml 在颜色文件夹中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_checked="true" />
<item android:color="@color/black" />
</selector>
在 values 文件夹中创建 attr.xml,然后在 attr.xml
中添加 CustomChipChoiceStyle 属性
<resources>
<attr name="CustomChipChoiceStyle" format="reference"/>
</resources>
在 theme.xml
文件中的当前 activity 主题中添加 CustomChipChoiceStyle
<item name="CustomChipChoiceStyle">@style/ChipCustomStyle</item>
现在将 CustomChipChoiceStyle
样式添加到您的 Chip in Kotlin 文件中,示例代码:
val chipGroup = findViewById<ChipGroup>(R.id.chipGroup)
for (name in names) {
val chip = Chip(this, null, R.attr.CustomChipChoiceStyle)
//val chip = Chip(this)
chip.text = name
chipGroup.addView(chip)
}
在我的 XML 中,我只是声明一个 ChipGroup
如下:
<com.google.android.material.chip.ChipGroup
android:id="@+id/chipGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
然后动态添加每个 Chip
:
ChipGroup chipGroup = findViewById(R.id.chipGroup);
for (String name : names) {
Chip chip = new Chip(activity);
chip.setText(name);
chipGroup.addView(chip);
}
所选筹码的颜色(下面的浅绿色)似乎来自 colorSecondaryContainer
属性(我知道这一点,因为当我在我的主题中更改它时,筹码颜色会改变)。我宁愿它来自 colorPrimaryContainer
,但按原样也可以,只是文本颜色不适合 colorSecondaryContainer
...特别是,文本颜色 不适合 似乎如我所料来自 colorOnSecondaryContainer
,因为 colorOnSecondaryContainer
在我的主题中是深色的,但我看到的是浅色芯片颜色上的浅色文字(和无论选中状态如何,文本颜色都是浅色的):
如何使我的 Chip 样式总体上符合我的 material 主题,而不必通过 chip.setTextColor()
等设置器来设置这些东西?我没有通过 XML 声明任何 Chip,所以我也无法覆盖单独声明中的样式。
在 theme.xml
文件中添加 Chip 样式
theme.xml
中的 ChipCustomStyle<style name="ChipCustomStyle" parent="@style/Widget.MaterialComponents.Chip.Choice">
<item name="chipBackgroundColor">@color/chip_background_color</item>
<item name="android:textColor">@color/chip_text_color</item>
<item name="chipCornerRadius">5dp</item>
</style>
在 res
下创建 color
资源文件夹并添加 chip_background_color.xml
用于芯片背景颜色和 chip_text_color.xml
用于芯片文本颜色
chip_background_color.xml 在颜色文件夹中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/green" android:state_checked="true" />
<item android:color="@color/white" />
</selector>
chip_text_color.xml 在颜色文件夹中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_checked="true" />
<item android:color="@color/black" />
</selector>
在 values 文件夹中创建 attr.xml,然后在 attr.xml
中添加 CustomChipChoiceStyle 属性<resources>
<attr name="CustomChipChoiceStyle" format="reference"/>
</resources>
在 theme.xml
文件中的当前 activity 主题中添加 CustomChipChoiceStyle
<item name="CustomChipChoiceStyle">@style/ChipCustomStyle</item>
现在将 CustomChipChoiceStyle
样式添加到您的 Chip in Kotlin 文件中,示例代码:
val chipGroup = findViewById<ChipGroup>(R.id.chipGroup)
for (name in names) {
val chip = Chip(this, null, R.attr.CustomChipChoiceStyle)
//val chip = Chip(this)
chip.text = name
chipGroup.addView(chip)
}