Android TextView 设置文字背景色或文字颜色,字体阴影,字体样式

      String str="这是设置TextView部分文字背景颜色和前景颜色的demo!";

        int bstart=str.indexOf("背景");

        int bend=bstart+"背景".length();

        int fstart=str.indexOf("前景");

        int fend=fstart+"前景".length();

        SpannableStringBuilder style=new SpannableStringBuilder(str);

        style.setSpan(new BackgroundColorSpan(Color.RED),bstart,bend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  

        style.setSpan(new ForegroundColorSpan(Color.RED),fstart,fend,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        TextView tvColor=(TextView) findViewById(R.id.tv_color);

        tvColor.setText(style);

 

动态TextView文本内容:

上期抄表%1$s度,本期抄表%2$s度
textView1.setText(getResources().getString(R.string.str_text1,120+"",200+""));

 

  private void setSpann(){
        //1ForegroundColorSpan 文本颜色(前景色)
        SpannableString spannableString = new SpannableString("我是一个无聊的文本,啦啦啦啦啦啦");
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#0099EE"));
        spannableString.setSpan(colorSpan, 9, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textView3.append("\n1");
        textView3.setText(spannableString);

        //2BackgroundColorSpan 背景色
        spannableString.setSpan(new BackgroundColorSpan(Color.parseColor("#0099EE")), 6, spannableString.length(), spannableString.SPAN_INCLUSIVE_EXCLUSIVE);
        textView3.append("\n2");
        textView3.append(spannableString);

       // 3. ClickableSpan: 点击事件相关的Span。
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, "click", Toast.LENGTH_SHORT).show();
                Log.e("Easy", "click");
            }
        };
        spannableString.setSpan(clickableSpan, 7, 11, Spannable.SPAN_INCLUSIVE_INCLUSIVE) ;
        textView3.setMovementMethod(LinkMovementMethod.getInstance());
        textView3.append("\n3");
        textView3.append(spannableString);

        //4MaskFilterSpan 修饰效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)
        int length = spannableString.length();
        //模糊(BlurMaskFilter)
        MaskFilterSpan maskFilterSpan = new MaskFilterSpan(new BlurMaskFilter(3, BlurMaskFilter.Blur.OUTER));
        spannableString.setSpan(maskFilterSpan, 0, length - 10, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        //浮雕(EmbossMaskFilter)
        maskFilterSpan = new MaskFilterSpan(new EmbossMaskFilter(new float[]{1,1,3}, 1.5f, 8, 3));
        spannableString.setSpan(maskFilterSpan, length - 10, length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textView3.append("\n4");
        textView3.append(spannableString);

     //   5、MetricAffectingSpan 父类,一般不用

        //  RasterizerSpan 光栅效果
//        spannableString.setSpan(new RasterizerSpan(), 0, 7, Spannable.
//                SPAN_INCLUSIVE_EXCLUSIVE);
//        textView3.append("\n");
//        textView3.append(spannableString);


        // 7StrikethroughSpan 删除线(中划线)
        spannableString.setSpan(new StrikethroughSpan(), 0, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        textView3.append("\n7");
        textView3.append(spannableString);

//        8、SuggestionSpan
//        相当于占位符,一般用在EditText输入框中。当双击此文本时,会弹出提示框选择一些建议(推荐的)文字,选中的文本将替换此占位符。在输入法上用的较多。
//        PS:API 14新增,暂无示例。

        //9UnderlineSpan 下划线
        spannableString.setSpan(new UnderlineSpan(), 0, spannableString.length(),
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        textView3.append("\n9");
        textView3.append(spannableString);

        // 10AbsoluteSizeSpan 绝对大小(文本字体)
        spannableString.setSpan(new AbsoluteSizeSpan(20, true), 0, 7,
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        textView3.append("\n10");
        textView3.append(spannableString);


     //   11、DynamicDrawableSpan 设置图片,基于文本基线或底部对齐。
        DynamicDrawableSpan drawableSpan =
                new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BASELINE) {
                    @Override
                    public Drawable getDrawable() {
                        Drawable d = getResources().getDrawable(R.mipmap.ic_launcher);
                        d.setBounds(0, 0, 50, 50);
                        return d;
                    }
                };
        DynamicDrawableSpan drawableSpan2 = new DynamicDrawableSpan(
                DynamicDrawableSpan.ALIGN_BOTTOM) {
            @Override
            public Drawable getDrawable() {
                Drawable d = getResources().getDrawable(R.mipmap.ic_launcher);
                d.setBounds(0, 0, 50, 50);
                return d;
            }
        };
        spannableString.setSpan(drawableSpan, 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(drawableSpan2, 7, 8, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        textView3.append("\n11");
        textView3.append(spannableString);


        // 12ImageSpan 图片
        Drawable d = getResources().getDrawable(R.mipmap.ic_launcher);
        d.setBounds(0, 0, 50, 50);
        spannableString.setSpan(new ImageSpan(d), 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        textView3.append("\n12");
        textView3.append(spannableString);

        //13、RelativeSizeSpan 相对大小(文本字体)
        //参数proportion:比例大小
        spannableString.setSpan(new RelativeSizeSpan(2.5f), 3, 4,
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        textView3.append("\n13");
        textView3.append(spannableString);

       // 14、ReplacementSpan 父类,一般不用

        // 15、ScaleXSpan 基于x轴缩放
        // 参数proportion:比例大小
        spannableString.setSpan(new ScaleXSpan(3.8f), 3, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        textView3.append("\n15");
        textView3.append(spannableString);

        // 16、StyleSpan 字体样式:粗体、斜体等
        //Typeface.BOLD_ITALIC:粗体+斜体
        spannableString.setSpan(new StyleSpan(Typeface.BOLD), 3, 7,
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        textView3.append("\n16");
        textView3.append(spannableString);

        // 17、SubscriptSpan 下标(数学公式会用到)
        spannableString.setSpan(new SubscriptSpan(), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        textView3.append("\n17");
        textView3.append(spannableString);

        // 18、SuperscriptSpan 上标(数学公式会用到)
        spannableString.setSpan(new SuperscriptSpan(), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        textView3.append("\n18");
        textView3.append(spannableString);

        // 19、TextAppearanceSpan 文本外貌(包括字体、大小、样式和颜色)
        //若需自定义TextAppearance,可以在系统样式上进行修改
        spannableString.setSpan(new TextAppearanceSpan(this, android.R.style.TextAppearance_Medium),
                6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        textView3.append("\n19");
        textView3.append(spannableString);

        // 20、TypefaceSpan 文本字体
        //若需使用自定义字体,可能要重写类TypefaceSpan
        spannableString.setSpan(new TypefaceSpan("monospace"), 3, 10,
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        textView3.append("\n20");
        textView3.append(spannableString);

        //  21、URLSpan 文本超链接
        spannableString.setSpan(new URLSpan("http://orgcent.com"), 10, spannableString.length(),
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        textView3.setMovementMethod(new LinkMovementMethod());
        textView3.append("\n21");
        textView3.append(spannableString);
        //让URLSpan可以点击
    }

部分字体着色

代码:

String sms = "审计系统在线";
String sjs = "审计系统在线";
int fstart = 4;
int fend = 6;
if (sms.contains("在线")) {
    SpannableStringBuilder style = new SpannableStringBuilder(sms);
    style.setSpan(new ForegroundColorSpan(Color.rgb(166, 116, 50)), fstart, fend, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    nethomesmstatus.setText(style);
} else {
    nethomesmstatus.setText(sms);
}

 

效果:

Android TextView 设置文字背景色或文字颜色,字体阴影,字体样式_第1张图片

 

 

字体阴影

textView.setShadowLayer(2,6,20,R.color.colorPrimary);//清晰度,偏移量,最后一个无效参数

 

字体样式

字体类型
Typeface.DEFAULT:默认字体,常规字体类型
Typeface.DEFAULT_BOLD:黑体字体类型
Typeface.MONOSPACE:等宽字体类型
Typeface.SANS_SERIF:sans serif字体类型
字体样式
Typeface.BOLD //粗体
Typeface.BOLD_ITALIC //粗斜体
Typeface.ITALIC //斜体
Typeface.NORMAL //常规
 

 

textView.setTypeface(Typeface.DEFAULT, Typeface.BOLD_ITALIC );

 

 //下划线 

textView.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );

//抗锯齿

textView.getPaint().setAntiAlias(true);

//中划线
textview.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG); 

// 设置中划线并加清晰 
textview.setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);  

 

holder.odlte.setPaintFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);

//字体加粗

textView.getPaint().setFakeBoldText(true);
 

你可能感兴趣的:(移动开发)