我有一个项目,用户选择了一个芯片,在这个背景下,我创建了一个后台xml文件,其中包含了所选的state= true和false。另外,当设置该文件的回溯颜色时,在此圆周率下不设置所选颜色,您可以在下面找到示例代码;
我以编程方式设置的片段:
private fun createChip(label: String, time: String, id: Int): Chip {
val chip = Chip(context, null, R.style.CustomChipStyle)
chip.chipBackgroundColor = ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.chip_background_color))
chip.layoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
chip.text = label
chip.isCheckable = true
chip.isClickable = true
chip.setOnClickListener {
binding.lytSelectTime.removeAllViews()
binding.lytSelectTime
val scrollView = HorizontalScrollView(context)
val chipGroup = ChipGroup(context)
chipGroup.layoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
芯片backroundColor xml文件代码;
<item android:color="@color/corporate_green" android:state_checked="false"/>
<item android:color="@color/corporate_blue2" android:state_selected="true"/>
谢谢你的努力..。
发布于 2022-11-08 14:04:20
只需获得检查的Id,您就可以通过这个代码片段以编程方式更改背景。
chip.chipBackgroundColor = ColorStateList.valueOf(ContextCompat.getColor(context, R.color.yourColorForBackground))
chip.chipStrokeColor = ColorStateList.valueOf( ContextCompat.getColor(context, R.color.yourColorForBorder))
chip.setTextColor(ColorStateList.valueOf(ContextCompat.getColor(context,R.color.edWhite)))
chip.chipStrokeWidth = 2.0f
https://stackoverflow.com/questions/74361455
复制相似问题