首页 > 解决方案 > 带有锚链接的 TextView HTML

问题描述

我有一个带有外部和内部链接(页面导航)的 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.

标签: androidtextviewfromhtml

解决方案


将您的 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>

推荐阅读