Android TextView更改<a> background color

时间:2015-08-25 16:21:39

标签: android textview

I want to change background color of a link in textview which was created using following code:

String htmlStr = "Click here: <a href=\"http://stackoverflow.com\">SO</a>";
Spanned htmlSpanned = Html.fromHtml(htmlStr, this, null);
txtView.setText( htmlSpanned );

I used font tag but it didn't work.

2 个答案:

答案 0 :(得分:1)

尝试使用一些css代码
字体标签不能改变html链接的背景颜色, 试试这段代码:

String htmlStr = "Click here: <a style='background-color:#00ff00' href=\'http://stackoverflow.com\'>SO</a>";
Spanned htmlSpanned = Html.fromHtml(htmlStr, this, null);
txtView.setText( htmlSpanned );

答案 1 :(得分:0)

String htmlStr = "Click here: ";
String link = "<font color='blue'><a href=\'http://stackoverflow.com\'>SO</a></font>";
Spanned htmlSpanned = Html.fromHtml(htmlStr + link, this, null);
txtView.setText( htmlSpanned );

您可以使用字体标记添加颜色。也可以做类似的事情:

int сolor = getResources().getColor(R.color.label_color);
String сolorString = String.format("%X", labelColor).substring(2);
Html.fromHtml(String.format("<font color=\"#%s\">text</font>", сolorString),

这会抓取颜色资源并将其删除Alpha通道值。如果您想在项目中使用特定颜色

,则非常有用