TextView锚链接空间 [英] TextView anchor link space

查看:83
本文介绍了TextView锚链接空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个带有链接的TextView.我是用html和Java的一部分组合而成的:

I want to make a TextView with a link. I made it with combination of html and bit of java:

// used to enable link navigation on TextView
setMovementMethod(LinkMovementMethod.getInstance())

// TextView with link
<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="19dp"
    android:layout_marginTop="8dp"
    android:gravity="center"
    android:linksClickable="true"
    android:text="@string/link"/>

// @string/link
<string name="link">Test <a href="#">link</a></string>

但是仍然存在一个问题,实际链接文本之前的空格用下划线表示:

However there is still one issue, the space before actual link text is underlined like this:

为什么会这样,如何解决?

Why is that and how could it be fixed?

推荐答案

在字符串中使用 CDATA 以使用 HTML 标记并使用 Html.fromHtml()方法来设置文本.

Use CDATA in string to use HTML tags and use Html.fromHtml() method to set the text.

以下实现:

使用 Activity 类中的 Html.fromHtml()设置文本.

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(Html.fromHtml(getString(R.string.link)));
textView.setMovementMethod(LinkMovementMethod.getInstance());

strings.xml 中进行如下修改:

<string name="link">Test <![CDATA[<a href="#">link</a>]]></string>

这篇关于TextView锚链接空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆