无法将文本颜色状态列表添加到 Material Chip
Cannot add text color state list to Material Chip
我正在尝试将文本颜色状态添加到 Material Chip。它甚至不可点击。
layout.xml
<HorizontalScrollView
android:id="@+id/horizontalScrollView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.chip.ChipGroup
android:id="@+id/cgServiceList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleLine="true"
app:singleSelection="true" />
</HorizontalScrollView>
fragment.kt
val chipGroup = view.cgServiceList
for (x in 0..12) {
val chip = Chip(context, null, R.style.Widget_MaterialComponents_Chip_Choice)
chip.text = "Nail Care"
chip.setTextColor(resources.getColorStateList(R.color.txt_chip_state_list, null))
chip.isClickable = true
chipGroup.addView(chip)
}
R.color.txt_chip_state_list
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/black" />
<item android:color="@color/colorPrimary" android:state_checked="true" />
</selector>
切换状态列表中 <item>
元素的顺序:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorPrimary" android:state_checked="true" />
<item android:color="@color/black" />
</selector>
列表从上到下解析,并选择第一个匹配项。在您的版本中,无论实际状态如何,第一个都会匹配(因为它根本没有定义任何状态)。
问题在这里:
val chip = Chip(context, null, R.style.Widget_MaterialComponents_Chip_Choice)
通过这种方式,您不会使用 Widget.MaterialComponents.Chip.Choice
样式,因为您不能使用 构造函数val chip = Chip(context, null, R.style.xxxx)
分配样式,因为 第三个参数 不是样式,而是主题中的属性 R.attr.chipStyle
.
有关以编程方式更改 Chip 样式的更多详细信息,请同时检查 this answer.
在您的代码中,您使用 Chip
和 Widget.MaterialComponents.Chip.Action
的默认样式,它提供:
<item name="android:checkable">false</item>
这解释了为什么您的 Chip
不可检查。
方法 chip.setTextColor
是正确的并且可以使用选择器(但使用 中 Ben.P 描述的选择器)。
正如您所指出的,如果您在 layout.xml 中使用它,它会起作用,因为在这种情况下,布局中声明的样式是正确的 (Widget.MaterialComponents.Chip.Choice
)。
解决方案:
有几种状态可以使用,Chip.Choice
默认是
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary" android:state_enabled="true" android:state_selected="true"/>
<item android:color="?attr/colorPrimary" android:state_enabled="true" android:state_checked="true"/>
<item android:alpha="0.87" android:color="?attr/colorOnSurface" android:state_enabled="true"/>
<item android:alpha="0.33" android:color="?attr/colorOnSurface"/>
</selector>
因此,您可以更改 colorPrimary
或通过设置 android:textColor="@color/color_state_list"
提供另一个类似的颜色列表
为什么?
调查<style name="Widget.MaterialComponents.Chip.Choice">
你可以看到它有
<item name="android:textColor">@color/mtrl_choice_chip_text_color</item>
然后查看 mtrl_choice_chip_text_color.xml
你可以找到我上面提供的格式。
我正在尝试将文本颜色状态添加到 Material Chip。它甚至不可点击。
layout.xml
<HorizontalScrollView
android:id="@+id/horizontalScrollView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.chip.ChipGroup
android:id="@+id/cgServiceList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleLine="true"
app:singleSelection="true" />
</HorizontalScrollView>
fragment.kt
val chipGroup = view.cgServiceList
for (x in 0..12) {
val chip = Chip(context, null, R.style.Widget_MaterialComponents_Chip_Choice)
chip.text = "Nail Care"
chip.setTextColor(resources.getColorStateList(R.color.txt_chip_state_list, null))
chip.isClickable = true
chipGroup.addView(chip)
}
R.color.txt_chip_state_list
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/black" />
<item android:color="@color/colorPrimary" android:state_checked="true" />
</selector>
切换状态列表中 <item>
元素的顺序:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorPrimary" android:state_checked="true" />
<item android:color="@color/black" />
</selector>
列表从上到下解析,并选择第一个匹配项。在您的版本中,无论实际状态如何,第一个都会匹配(因为它根本没有定义任何状态)。
问题在这里:
val chip = Chip(context, null, R.style.Widget_MaterialComponents_Chip_Choice)
通过这种方式,您不会使用 Widget.MaterialComponents.Chip.Choice
样式,因为您不能使用 构造函数val chip = Chip(context, null, R.style.xxxx)
分配样式,因为 第三个参数 不是样式,而是主题中的属性 R.attr.chipStyle
.
有关以编程方式更改 Chip 样式的更多详细信息,请同时检查 this answer.
在您的代码中,您使用 Chip
和 Widget.MaterialComponents.Chip.Action
的默认样式,它提供:
<item name="android:checkable">false</item>
这解释了为什么您的 Chip
不可检查。
方法 chip.setTextColor
是正确的并且可以使用选择器(但使用
正如您所指出的,如果您在 layout.xml 中使用它,它会起作用,因为在这种情况下,布局中声明的样式是正确的 (Widget.MaterialComponents.Chip.Choice
)。
解决方案:
有几种状态可以使用,Chip.Choice
默认是
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary" android:state_enabled="true" android:state_selected="true"/>
<item android:color="?attr/colorPrimary" android:state_enabled="true" android:state_checked="true"/>
<item android:alpha="0.87" android:color="?attr/colorOnSurface" android:state_enabled="true"/>
<item android:alpha="0.33" android:color="?attr/colorOnSurface"/>
</selector>
因此,您可以更改 colorPrimary
或通过设置 android:textColor="@color/color_state_list"
为什么?
调查<style name="Widget.MaterialComponents.Chip.Choice">
你可以看到它有
<item name="android:textColor">@color/mtrl_choice_chip_text_color</item>
然后查看 mtrl_choice_chip_text_color.xml
你可以找到我上面提供的格式。