android TextView添加标签,一个或多个

不需要自定义View给TextView添加标签。

build.gradle添加
implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'

代码块:
public class TestLableActivity extends Activity {
    TextView tvLable;
    TextView tvLable2;
    private int width;
    private int height;
    private String lableStr = "标签1";
    private String lableStr2 = "标签2";
    private String content = "内容显示内容显示内容显示内容显示内容显示内容显示内容显示内容显示内容显示内容显示";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_textlable);
        tvLable = findViewById(R.id.tv_lable);
        tvLable2 = findViewById(R.id.tv_lable2);
        width = getResources().getDisplayMetrics().widthPixels;
        height = getResources().getDisplayMetrics().heightPixels;
        height = height / 38;
        width = width / 25;
        Drawable drawable = TextDrawable.builder()
                .beginConfig()
                .width(lableStr.length() * width)  // width in px
                .height(height) // height in px
                .textColor(Color.parseColor("#5b3719"))
                .fontSize((int) (height * 0.7))
                .endConfig()
                .buildRoundRect(lableStr, Color.parseColor("#eea79b"), 5);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        setTextStyle(content, drawable, tvLable); //添加一个标签
        Drawable drawable2 = TextDrawable.builder()
                .beginConfig()
                .width(lableStr2.length() * width)  // width in px
                .height(height) // height in px
                .textColor(Color.parseColor("#5b3719"))
                .fontSize((int) (height * 0.7))
                .endConfig()
                .buildRoundRect(lableStr2, Color.parseColor("#eea79b"), 5);
        drawable2.setBounds(0, 0, drawable2.getMinimumWidth(), drawable2.getMinimumHeight());
        setTextStyle(content, drawable, drawable2, tvLable2); //添加两个标签
    }

    /**
     * 利用SpannableString实现添加Drawable
     *
     * @param content
     * @param drawable
     * @param tv
     */
    private void setTextStyle(String content, Drawable drawable, TextView tv) {
        SpannableString spanText = new SpannableString(" " + " " + " " + content);
        // 替换0,1的字符
        if (drawable != null) {
            spanText.setSpan(new ImageSpan(drawable), 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        } else {
            spanText.setSpan("", 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        tv.setText(spanText);
    }

    private void setTextStyle(String content, Drawable drawable, Drawable drawable2, TextView tv) {
        SpannableString spanText = new SpannableString(" " + " " + " " + " " + content);
        // 替换0,1的字符
        if (drawable != null) {
            spanText.setSpan(new ImageSpan(drawable), 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        } else {
            spanText.setSpan("", 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }

        if (drawable2 != null) {
            spanText.setSpan(new ImageSpan(drawable2), 2, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        } else {
            spanText.setSpan("", 2, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        tv.setText(spanText);
    }

}

 效果图:

android TextView添加标签,一个或多个_第1张图片

你可能感兴趣的:(android TextView添加标签,一个或多个)