To implement the underline and color settings:
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.getpaint ( ). SetFlags (Paint.underline_text_flag);//underline
tv.settext ("Use code for underline style");
Tv.settextcolor (color.white);
Ll.addview (TV);
tv=new TextView (this);
Tv.settext (html.fromhtml ("<u> using Html for underline style </u>"));
Ll.addview (TV);
}
}
Set Hyperlink style: The default hyperlink is blue, we are now set to foreground red
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\ "> Custom hyperlink Style </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);//set foreground color to red
}
Tv.settext (style);
}
Ll.addview (TV);
Tv=new TextView (this);
Tv.settext (html.fromhtml ("<a href=\" http://blog.csdn.net/caiyunfreedom\ "> Default hyperlink Style </a>"));
Tv.setmovementmethod (Linkmovementmethod.getinstance ());
Ll.addview (TV);
}
}
Android: Add style to TextView-underline, color, set link style and front background color