zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    [计算机网络-传输层] 无连接传输:UDP
    [BinaryTree] 最大堆的类实现
    [OS] 生产者-消费者问题(有限缓冲问题)
    [剑指Offer] 64.滑动窗口的最大值
    [剑指Offer] 63.数据流中的中位数
    [剑指Offer] 62.二叉搜索树的第k个结点
    [OS] CPU调度
    [剑指Offer] 60.把二叉树打印成多行
    MySQL数据库实验二:单表查询
    数据库实验:基本表的定义与修改
  • 原文地址:https://www.cnblogs.com/tt_mc/p/1751650.html
Copyright © 2011-2022 走看看