Android TextView 少数文字字体颜色格式

Android TextView 个别文字字体颜色格式
Android TextView 个别文字字体颜色格式
1.简单的办法,用Html来格式化字符
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;

public class AndroidFronColorTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        
        TextView htmlFormateTextView = (TextView)findViewById(R.id.testTextView);
        
        String source = "这只是一个测试,测试<u>下划线</u>、<i>斜体字</i>、<font color='red'>红色字</font>的格式";
 
        htmlFormateTextView.setText(Html.fromHtml(source));
    }
}


Android TextView 少数文字字体颜色格式

方法2
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.style.BackgroundColorSpan;
import android.text.style.StyleSpan;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidFronColorTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        
        TextView htmlFormateTextView = (TextView)findViewById(R.id.testTextView);
        
        String source = "这只是一个测试,测试<u>下划线</u>、<i>斜体字</i>、<font color='red'>红色字</font>的格式";
 
        htmlFormateTextView.setText(Html.fromHtml(source));
        
        EditText et = (EditText) findViewById(R.id.textView);

        Spannable sp = (Spannable) et.getText();

        sp.setSpan(new BackgroundColorSpan(Color.RED), 0, 5,

        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 6, 11,

        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}


Android TextView 少数文字字体颜色格式
1 楼 Stero 2011-01-06  
很给力,谢谢!Android TextView 少数文字字体颜色格式
2 楼 Jaycee 2011-06-27  
厉害!Android TextView 少数文字字体颜色格式 Android TextView 少数文字字体颜色格式
3 楼 Tank03 2011-12-29  
很感谢楼主啊。正在找