如何为autoLink="web"的链接添加样式?

3

我有以下TextView:

<TextView android:id="@+id/theFooBar"
          android:autoLink="web"
          android:layout_height="wrap_content"
          android:layout_width="fill_parent"
          android:text="@string/fooBar"
          android:textColor="@android:color/black"
          android:textSize="20sp"/>

And the string:

<string name="fooBar">Foo <u>bar</u>.</string>

这会使我的文本变成黑色的下划线。如果我想要链接(“bar”部分)为蓝色非下划线文本,但是我希望其余部分(“foo”部分)为黑色,我该怎么做呢?如何实现?

2个回答

4
在TextView中添加android:textColorLink以定义链接文本的颜色。

1
我建议您在这种情况下使用WebView而不是TextView:
WebView web = (WebView) findViewById(R.id.theFooBar);
String str = "<font color='blue'>bar</font><font color='black'><u>foo</u></font>";
web.setBackgroundColor(0);
// It will sets the background color from white to transparent. 
web.loadData(str, "text/html", "utf8");

这似乎可以工作,但我该如何处理点击事件?我调用了 setOnClickListener(),但当我点击 WebView 时,我的回调从未被调用。 - i_am_jorf
是的,使用 WebView 可能会起作用,但仅仅为了使一些文本颜色不同而增加太多复杂性。特别是涉及本地化时。我最终将两个文本视图放在一起。 - i_am_jorf
你可以使用JavaScript来处理点击事件。但是,如果所有的问题都在于你想要为链接(“bar”部分)使用“非下划线文本”,但是你希望其余部分(“foo”部分)是黑色的,那么最好的解决方案是使用两个文本视图。我已经给出了更广泛的解决方案,想象一下你必须做比所需更多的事情。 - George

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接