You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中如何通过代码设置Chip的选中图标及Drawable资源?

代码设置Chip选中态图标(API 34)

直接用setIcon()无效是因为这个方法设置的是Chip的普通前置/后置图标,而你要替换的选中态对勾属于checked icon,得用Chip专门的setCheckedIcon()方法。

Fragment里的实现示例:

// 先获取Chip实例(用ViewBinding或findViewById均可)
val myChip = view.findViewById<Chip>(R.id.my_chip)
// 加载drawable文件夹中的XML资源
val customCheckedIcon = ContextCompat.getDrawable(requireContext(), R.drawable.my_xml_asset_name)
// 设置选中态图标
myChip.setCheckedIcon(customCheckedIcon)

额外提醒:

  • 若图标显示尺寸不合适,可调用setCheckedIconSize()调整大小
  • 确保你的XML drawable资源格式正确(如vector、shape类型),能被Chip正常加载

内容的提问来源于stack exchange,提问作者Hele

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 04:43:12