Android TextView HTML文本在行尾换行
创始人
2024-08-19 00:30:05
0

要在Android TextView中实现HTML文本在行尾换行,可以使用以下代码示例:

  1. 首先,在XML布局文件中定义一个TextView控件:

  1. 在Java代码中,获取并设置HTML文本到TextView:
TextView textView = findViewById(R.id.textView);
String htmlText = "This is a long line of text that needs to be wrapped at the end of the line. This is a long line of text that needs to be wrapped at the end of the line. This is a long line of text that needs to be wrapped at the end of the line.";
textView.setText(Html.fromHtml(htmlText));
  1. 创建一个自定义的HtmlTagHandler类来处理HTML标签,并在标签结尾处添加一个换行符:
import android.text.Editable;
import android.text.Html;
import android.text.Spannable;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
import android.view.View;

import org.xml.sax.XMLReader;

public class HtmlTagHandler implements Html.TagHandler {

    private static final String TAG_BR = "br";

    @Override
    public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
        if (tag.equalsIgnoreCase(TAG_BR) && !opening) {
            output.append("\n");
        }
    }
}
  1. 修改Java代码中的TextView设置,以使用自定义的HtmlTagHandler:
TextView textView = findViewById(R.id.textView);
String htmlText = "This is a long line of text that needs to be wrapped at the end of the line. This is a long line of text that needs to be wrapped at the end of the line. This is a long line of text that needs to be wrapped at the end of the line.";

// 设置自定义的HtmlTagHandler
HtmlTagHandler htmlTagHandler = new HtmlTagHandler();
textView.setText(Html.fromHtml(htmlText, null, htmlTagHandler));

通过这个解决方法,HTML文本将在TextView中的行尾处自动换行。

相关内容

热门资讯

专业讨论!wepoker透视脚... 专业讨论!wepoker透视脚本下载,德普之星私人局辅助免费,玩家教程(有挂辅助);人气非常高,ai...
一秒答解!hh poker插件... 1、一秒答解!hh poker插件下载,wejoker开挂,存在挂教程(有挂方法)(UU poker...
科技分享!hh poker插件... 科技分享!hh poker插件下载,德普之星辅助器,AI教程(有挂技巧);实战中需综合运用上述技巧,...
2024教程!we-poker... 2024教程!we-poker软件,wepoker私人局俱乐部,2025新版总结(有挂教程)1、不需...
盘点十款!pokemomo辅助... 盘点十款!pokemomo辅助软件,hhpoker为什么一直输,力荐教程(有挂方法)1、点击下载安装...
今日科普!哈糖大菠萝有没有挂,... 今日科普!哈糖大菠萝有没有挂,aapoker破解侠是真的吗,科技教程(有挂神器);小薇(透视辅助)致...
盘点一款!wejoker私人辅... 盘点一款!wejoker私人辅助软件,智星德州插件怎么下载,可靠教程(有挂神器);《WPK辅助透视》...
我来分享!pokemmo手机脚... 我来分享!pokemmo手机脚本,wepoker怎么拿到好牌,线上教程(有挂神器);玩家必备必赢加哟...
实测必看!aapoker怎么设... 实测必看!aapoker怎么设置提高好牌几率,werplan外挂,必胜教程(有挂辅助)准备好在aap...
记者发布!steampoker... 1、记者发布!steampokermaster辅助,cloudpoker怎么开挂,分享教程(有挂透明...