需求:根据给定的文字(代码中用户动态输入,或接口固定返回等),展示标签样式,且只展示第一个字。如:经济。则,标签展示“经”
扩展功能:文字随控件大小,对应变化,不需要额外设置。
样式图:
代码:
1、res -> values -> styles 中
<!--标签View的自定义属性-->
<declare-styleable name="LabelTextView">
<!--标签内要展示的文字-->
<attr name="labelText" format="string"/>
<!--标签的背景颜色-->
<attr name="labelBgColor" format="color"/>
<!--标签中的文字的颜色-->
<attr name="labelTextColor" format="color"/>
<!--标签的大小,单位:dp。因为宽=高,所以,指定一个就行-->
<attr name="labelSize" format="integer"/>
</declare-styleable>
2、标签布局源码:LabelTextView
package com.demo.customtextdemo
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.text.TextPaint
import android.ut