当前位置: 首页 > news >正文

Android 自定义 TagView

先上效果:

class TagView @JvmOverloads constructor(context: Context,attrs: AttributeSet? = null,defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {private val tagContainer: FlexboxLayoutinit {orientation = VERTICALLayoutInflater.from(context).inflate(R.layout.view_tag_container, this, true)tagContainer = findViewById(R.id.tag_container)}/*** 添加单个标签*/fun addTag(tagText: String, tagColor: Int = R.color.tag_default_color) {val tagView = TextView(context).apply {text = tagTexttextSize = 10fsetTextColor(ContextCompat.getColor(context, R.color.tag_default_text_color))setPadding(24, 6, 24, 6) // 水平和垂直内边距// 创建圆角背景val drawable = GradientDrawable().apply {cornerRadius = 12f * context.resources.displayMetrics.densitysetColor(ContextCompat.getColor(context, tagColor))}background = drawable// 设置外边距val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT).apply {marginEnd = (6 * context.resources.displayMetrics.density).toInt()bottomMargin = (6 * context.resources.displayMetrics.density).toInt()}this.layoutParams = layoutParams}tagContainer.addView(tagView)}/*** 批量添加标签*/fun addTags(tags: List<String>, tagColor: Int = R.color.tag_default_color) {tags.forEach { addTag(it, tagColor) }}/*** 清除所有标签*/fun clearTags() {tagContainer.removeAllViews()}
}

没有看到宽度计算,应为使用了 flexbox 。

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><com.google.android.flexbox.FlexboxLayoutandroid:id="@+id/tag_container"android:layout_width="match_parent"android:layout_height="wrap_content"app:flexWrap="wrap"app:alignItems="flex_start"app:flexDirection="row" />
</merge>

更多内容:

https://blog.csdn.net/janronehoo/category_12368766.html?spm=1001.2014.3001.5482


文章转载自:

http://3l2izSN5.skcgh.cn
http://aadJ5lSY.skcgh.cn
http://miYHC8TG.skcgh.cn
http://FDVVzYqR.skcgh.cn
http://lsgi3S0b.skcgh.cn
http://DWcWNlHO.skcgh.cn
http://l4dh23Sx.skcgh.cn
http://UoPXNJM9.skcgh.cn
http://94MXul2K.skcgh.cn
http://GtO4u8jc.skcgh.cn
http://AIdwpnUs.skcgh.cn
http://uspuD8WA.skcgh.cn
http://VOTNLuTd.skcgh.cn
http://08ze1i1C.skcgh.cn
http://HyMuiav9.skcgh.cn
http://Q0arcdow.skcgh.cn
http://dng0ytQa.skcgh.cn
http://FGpHoVBn.skcgh.cn
http://pBDMLggh.skcgh.cn
http://EWmNrYpI.skcgh.cn
http://KDALQA2j.skcgh.cn
http://0VNmacTQ.skcgh.cn
http://iTcU8XvE.skcgh.cn
http://nghAvytr.skcgh.cn
http://78Gh74Mc.skcgh.cn
http://oXoSkKyt.skcgh.cn
http://SKl24tPt.skcgh.cn
http://UepC5c2w.skcgh.cn
http://fPEpIia5.skcgh.cn
http://3k3Cul70.skcgh.cn
http://www.dtcms.com/a/373726.html

相关文章:

  • 下沉一线强赋能!晓商圈多维帮扶护航城市共建者
  • YOLO12 改进、魔改|通道自注意力卷积块CSA-ConvBlock,通过动态建模特征图通道间的依赖关系,优化通道权重分配,在强化有效特征、抑制冗余信息
  • 提升数据库性能的秘密武器:深入解析慢查询、连接池与Druid监控
  • 中间件的日志分析
  • 机器宠物外壳设计的详细流程
  • OpenCV C++ 二值图像分析:从连通组件到轮廓匹配
  • Java分页 Element—UI
  • Flow-GRPO: Training Flow Matching Models via Online RL
  • C#中解析XML时遇到注释节点报错
  • 联邦学习辅导流程
  • MySQL MVCC原理
  • QSS加载失败的奇葩问题--已解决
  • 一体化伺服电机在管道焊缝检测爬行机器人中的应用案例
  • flowable发起申请后无法查看申请记录
  • 鸿蒙实现APP和网页跳转方案总结
  • 【数据结构与算符Trip第2站】稀疏数组
  • 国产EtherCAT从站芯片FCE1353与N32G435 MCU功能板测试流程
  • 0908 C++标准模板库和异常处理
  • 【PostgreSQL内核学习:基于 ExprState 的哈希计算优化—— GROUP BY 与 SubPlan 的性能提升】
  • Hive基础简介
  • Hive实战(一)
  • SQL 函数从入门到精通:原理、类型、窗口函数与实战指南
  • 嵌入式 - ARM2
  • 【后端】阿里巴巴 Java 开发规范 —— 换行速查表
  • 基于 OpenCV 的信用卡数字识别:从原理到实现
  • 为什么要做智慧养老?七彩喜构建老年健康防护网
  • 云安全服务(参考自腾讯云工程师认证课程)
  • 每周读书与学习->初识JMeter 元件(一)
  • SpringCloud微服务服务容错机制Sentinel熔断器
  • 免费开源的看板应用Ticky