[转载]Android TextView 个别文字字体颜色格式 - GhostFromHeaven - ITeye技术网站

[转载]Android TextView 个别文字字体颜色格式 – GhostFromHeaven – ITeye技术网站.

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 = "这只是一个测试,测试<span style="text-decoration: underline;">下划线</span>、<i>斜体字</i>、<span style="color: red;">红色字</span>的格式";

htmlFormateTextView.setText(Html.fromHtml(source));
}
}

方法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 = "这只是一个测试,测试<span style="text-decoration: underline;">下划线</span>、<i>斜体字</i>、<span style="color: red;">红色字</span>的格式";

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);
}
}
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏