zoukankan      html  css  js  c++  java
  • Android TextView 富文本SpannableString的使用

    效果如下:

    代码如下:

        @Override
        protected void initView() {
            mContext=this;
            //中等字体
            int middleFontSize = (int) Utils.sp2px(mContext,18);
            //小字体
            int smallFontSize = (int) Utils.sp2px(mContext,18);
            //红色 #F65236
            int colorRed = ContextCompat.getColor(mContext, R.color._ff4902);
            //灰色 #999999
            int textColorThird = ContextCompat.getColor(mContext, R.color._6389cc);
            //灰色 #999999
            int textColor36 = ContextCompat.getColor(mContext, R.color._363636);
            ClickableSpan clickableSpan=new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    Toast.makeText(TopicWorksInformationActivity.this, "展开", Toast.LENGTH_SHORT).show();
                }
                @Override
                public void updateDrawState(TextPaint ds) {
                    ds.setColor(textColor36);
                    ds.setUnderlineText(false);
                }
            };
    
            SpannableStringBuilder spanBuilder = new SpannableStringBuilder();
    
            String price = "¥189";
            int start = 0;
            int end = price.length();
            spanBuilder.append(price);
            //添加字体大小样式span
            spanBuilder.setSpan(new AbsoluteSizeSpan(middleFontSize), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            //添加文本颜色样式span
            spanBuilder.setSpan(new ForegroundColorSpan(colorRed), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    
            String priceUnit = "次/人";
            start = end;
            end += priceUnit.length();
            spanBuilder.append(priceUnit);
            spanBuilder.setSpan(new AbsoluteSizeSpan(smallFontSize), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            spanBuilder.setSpan(new ForegroundColorSpan(textColor36), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    
            String line = " - 原价¥";
            start = end;
            end += line.length();
            spanBuilder.append(line);
            spanBuilder.setSpan(new AbsoluteSizeSpan(smallFontSize), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            spanBuilder.setSpan(new ForegroundColorSpan(textColorThird), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    
            String originalPricePrefix = "199";
            start = end;
            end += originalPricePrefix.length();
            spanBuilder.append(originalPricePrefix);
            spanBuilder.setSpan(new AbsoluteSizeSpan(smallFontSize), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            spanBuilder.setSpan(new ForegroundColorSpan(textColorThird), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            //添加删除线样式span
            spanBuilder.setSpan(new StrikethroughSpan(), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    
            String zhankai = "  展开";
            start = end;
            end += zhankai.length();
            spanBuilder.append(zhankai);
            spanBuilder.setSpan(new AbsoluteSizeSpan(smallFontSize), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            spanBuilder.setSpan(new ForegroundColorSpan(textColor36), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            spanBuilder.setSpan(clickableSpan,start,end,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    
    
    
            String kongge = " ";
            start = end;
            end += kongge.length();
            //获取一张图片
            Drawable drawable = this.getResources().getDrawable(R.mipmap.icon_skip);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            //居中对齐imageSpan
            CenterAlignImageSpan imageSpan = new CenterAlignImageSpan(drawable);
            spanBuilder.append(kongge);
            spanBuilder.setSpan(imageSpan,start,end,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    
    
            //最终把SpannableStringBuilder设置给TextView
            tvSynopsis.setText(spanBuilder);
            tvSynopsis.setHighlightColor(Color.TRANSPARENT); //设置点击后的颜色为透明
    
            tvSynopsis.setMovementMethod(LinkMovementMethod.getInstance());
        }

  • 相关阅读:
    Unity中将相机截图保存本地后颜色变暗的解决方法
    《无间道》亚索盲僧上演天台对决——开发者日志(二)_为人物添加动画
    《无间道》亚索盲僧上演天台对决——开发者日志(一)_项目启动
    CCF推荐期刊会议列表(2019第五版)——《中国计算机学会推荐国际学术会议和期刊目录》
    用3dMax给lol人物模型制作表情动画并导入Unity
    与100个诺手一起跨年
    C#发邮件
    SQL的各种连接Join详解
    Oracle&SQLServer中实现跨库查询
    SQL SERVER/ORACLE连接查询更简洁方便的方式
  • 原文地址:https://www.cnblogs.com/lixiangyang521/p/13850764.html
Copyright © 2011-2022 走看看