• textView文本不同颜色


    // 文本样式   
            EditText et = (EditText) findViewById(R.id.tv);   
            et.setText(
    "Styling the content of an editText dynamically");   
            Spannable sp 
    = et.getText();   
            sp.setSpan(
    new BackgroundColorSpan(Color.RED), 38,   
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);   //3代表从第几个字符开始变颜色,注意第一个字符序号是0.
    //8代表变色到第几个字符.

            sp.setSpan(
    new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 07,   
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
           textview.setText(sp);  
    java程序中动态改变TextView特定内容的Style或者实现更为复杂的效果,下边将使用Spannable介绍具体的实现方法,请参照以下代码设置:
    01.// Get our EditText object.
    02.EditText vw = (EditText)findViewById(R.id.text);
    03.

    04.// Set the EditText's text.
    05.vw.setText("Italic, highlighted, bold.");
    06.

    07.// If this were just a TextView, we could do:
    08.// vw.setText("Italic, highlighted, bold.", TextView.BufferType.SPANNABLE);
    09.// to force it to use Spannable storage so styles can be attached.
    10.// Or we could specify that in the XML.
    11.

    12.// Get the EditText's internal text storage
    13.Spannable str = vw.getText();
    14.

    15.// Create our span sections, and assign a format to each.
    16.str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 07, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    17.str.setSpan(new BackgroundColorSpan(0xFFFFFF00), 819, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    18.str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 21, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    和大家分享一下:
    TextView tv;
    tv.setText(
    "Test");
    Spannable span 
    = (Spannable)tv.getText();
    spn.setSpan(
    new BackgroundColor(#ffffffff), start, end,   Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    ps:start和end表示你要高亮显示的子串在Test中的起始和结束位置。
    go2android 发表于 
    2009-9-11 10:19

    忘了一项,setText的时候要用setText(
    "Text", TextView.BufferType.SPANNABLE);
  • 相关阅读:
    jquery validate常用方法及注意问题
    jQuery Validate
    $('div','li') 和 $('div , li') 和 $('div li') 区别
    HTML中Div、span、label标签的区别
    Java中访问修饰符public、private、protecte、default
    shiro过滤器过滤属性含义
    (转载)常用Git命令清单
    Git-简明指南
    菜鸟程序员怎么才能提高自己的技术--(献给自己共勉)
    Visiual Studio CLR20r3问题
  • 原文地址:https://www.cnblogs.com/tt_mc/p/1751650.html
Copyright © 2020-2023  润新知