android - 带有锚链接的 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.
解决方案
将您的 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>
推荐阅读
- react-native - 使用 npm 和 yarn 安装 expo-cli 时出错
- python - 在python中为对象赋值
- java - 使用 Java Stream API 在地图中查找重复值
- java - 如何将数据从属性对象保存到文件 + 如何将属性格式的文件加载到另一个方法中的属性对象?
- mysql - 跟踪存在
- robotframework - 如何使用机器人框架处理边缘浏览器的“您的连接不是私有的”
- javascript - 错误地将日期时间转换为角度与矩库中的角度
- javascript - 机器人是否可以为消息随机化 2 种颜色?
- git - 是否可以在 git config 中设置多个用户电子邮件?
- c++ - 如何将回调传递给 C++ 的 EM_ASM?