Android - 文本后面添加标签

 

 

原理:

定义一个view-》设置标签相关信息-大小,颜色,背景 -》 转换为bitmap -》 ImageSpan 对象-》 spannableStringBuilder 对象设置

给文本赋值:TextView .text =  spannableStringBuilder 对象

效果:

 

 

 

 

配合databinding实现:说明下面的 pd方法 为kt拓展函数 - 转为px

KT-先拓展标签方法 

object TextViewAdapter {

    //给标题 后面添加标签
    @BindingAdapter("textTag")
    @JvmStatic
    fun adaptTextTag(view: TextView, value: String?) {
        val text = value ?: "";
        if (text.isEmpty()) return

        val textList = text.split("@")
        if (textList.size > 1) {
            view.text = createTag(view, textList.get(0), textList.get(1))
        } else {
            view.text = text
        }
    }

    @JvmStatic
    private fun createTag(view: TextView, title: String, tag: String): SpannableStringBuilder {

        val mContext = view.context
        val content = title + tag
        val colorStr = "#ffff3b56"

        /**
         * 创建TextView对象,设置drawable背景,设置字体样式,设置间距,设置文本等
         * 这里我们为了给TextView设置margin,给其添加了一个父容器LinearLayout。不过他俩都只是new出来的,不会添加进任何布局
         */
        val layout = LinearLayout(mContext)
        val textView = TextView(mContext)
        textView.text = tag
        textView.background = mContext.resources.getDrawable(R.drawable.bg_msg_label)
        textView.textSize = 10f
        textView.setTextColor(Color.parseColor(colorStr))
        textView.includeFontPadding = false

        //内容左右内间距
        val hPadding = mContext.dp(6).toInt()
        val vPadding = mContext.dp(2).toInt()
        textView.setPadding(
            hPadding, vPadding,
            hPadding, vPadding
        )
        //textView.height = dip2px(30)
        textView.gravity = Gravity.CENTER_VERTICAL
        val layoutParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )

        val lMagrin = mContext.dp(5).toInt()
        val bMargin = mContext.dp(3).toInt()

        // 设置左间距
        layoutParams.leftMargin = lMagrin
        // 设置下间距,简单解决ImageSpan和文本竖直方向对齐的问题
        layoutParams.bottomMargin = bMargin
        layout.addView(textView, layoutParams)
        /**
         * 第二步,测量,绘制layout,生成对应的bitmap对象
         */
        layout.isDrawingCacheEnabled = true
        layout.measure(
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        )
        // 给上方设置的margin留出空间
        layout.layout(
            0,
            0,
            textView.measuredWidth + (lMagrin + bMargin),
            textView.measuredHeight
        )
        // 获取bitmap对象
        val bitmap = Bitmap.createBitmap(layout.drawingCache)
        //千万别忘最后一步
        layout.destroyDrawingCache()
        /**
         * 第三步,通过bitmap生成我们需要的ImageSpan对象
         */
        val imageSpan = ImageSpan(mContext, bitmap)

        /**
         * 第四步将ImageSpan对象设置到SpannableStringBuilder的对应位置
         */
        val ssb = SpannableStringBuilder(content)

        ssb.setSpan(imageSpan, title.length, content.length, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)

        return ssb

    }


}



然后标签使用
<TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:ellipsize="end"
                            android:maxLines="2"
                            android:textColor="#00000"
                            android:textSize="15sp"
                            app:textTag="@{`这里是测试标题@Official`}" />

  

 

 

 

 

参考:

DataBinding

https://juejin.cn/post/6844904186644004872#heading-0

 

文本后面添加标签:

https://blog.csdn.net/qq_26287435/article/details/100305849

https://github.com/tinyvampirepudge/Android_Base_Demo/blob/master/app/src/main/java/com/tiny/demo/firstlinecode/uicomponents/textview/SpannableStringBuilderActivity.java

 

https://juejin.cn/post/6964261957650579493#heading-4

 

posted @ 2021-12-09 14:22  naibox  阅读(445)  评论(0编辑  收藏  举报