實現下劃線及顏色設置: html
設置超連接樣式:默認的超連接是藍色的,咱們如今設置成前景紅色 ide
public class AtActivity extends Activity {
LinearLayout ll;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ll=(LinearLayout)findViewById(R.id.ll);
TextView tv=new TextView(this);
tv.setText(Html.fromHtml("<a href=\"http://blog.csdn.net/CAIYUNFREEDOM\">自定義的超連接樣式</a>"));
tv.setMovementMethod(LinkMovementMethod.getInstance());
CharSequence text = tv.getText();
if (text instanceof Spannable){
int end = text.length();
Spannable sp = (Spannable)tv.getText();
URLSpan[] urls = sp.getSpans( 0 , end, URLSpan.class );
SpannableStringBuilder style = new SpannableStringBuilder(text);
style.clearSpans(); // should clear old spans
for (URLSpan url : urls){
URLSpan myURLSpan= new URLSpan(url.getURL());
style.setSpan(myURLSpan,sp.getSpanStart(url),sp.getSpanEnd(url),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.RED), sp.getSpanStart(url), sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//設置前景色爲紅色
}
tv.setText(style);
}
ll.addView(tv);
tv=new TextView(this);
tv.setText(Html.fromHtml("<a href=\"http://blog.csdn.net/CAIYUNFREEDOM\">默認的超連接樣式</a>"));
tv.setMovementMethod(LinkMovementMethod.getInstance());
ll.addView(tv);
}
} ui