带有锚链接的 TextView HTML

时间:2021-01-12 13:26:57

标签: android textview fromhtml

我有一个 HTML 文本,其中包含外部和内部链接(页面导航)。当我在 Html.fromHtml 中使用此文本并将其显示在 TextView 中时,外部链接工作,但在页面本身内部,它不会去。我已经尝试过 setMovementMethod 和 Linkify - 一切都不对。我知道它通过 WebView 工作,但以这种方式工作会更快、更方便。

代码示例:

private void showText(String text) {
        TextView content = view.findViewById(R.id.textContent);
        content.setText(Html.fromHtml(text, FROM_HTML_MODE_COMPACT));
        content.setMovementMethod(LinkMovementMethod.getInstance());  
    }

文本示例:

The text is long, contained in a ScrollView, has <a name="sdfootnoteAanc" href="#sdfootnoteAsym"> links </a> within itself, such as footnotes. When pressed, the text should scroll to <a name="sdfootnoteAsym" href="#sdfootnoteAanc"> link location </a>, and then back - as often happens on sites.

1 个答案:

答案 0 :(得分:1)

将您的 TextView 放入 ScrollView。

<ScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none" >

    <TextView
        android:id="@+id/textContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</ScrollView>