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中的行尾处自动换行。

相关内容

热门资讯

透视ai!wepoker底牌透... 透视ai!wepoker底牌透视脚本,智星德州菠萝安装,终于懂了(有挂辅助挂)1、每一步都需要思考,...
微扑克辅助软件!德州免费辅助神... 微扑克辅助软件!德州免费辅助神器app,(wePOKE)其实是真的有挂,新2025版(有挂工具);1...
wpk有透视辅助!wepoke... wpk有透视辅助!wepokeai代打,(wEpOke)原来有挂,曝光教程(有挂插件)1、下载好wp...
透视神器!wepoker透视,... 透视神器!wepoker透视,hhpoker万能辅助器,热点推荐(有挂方法);软件透明挂微扑克wpk...
德州AI智能辅助机器人!微扑克... 德州AI智能辅助机器人!微扑克有辅助挂,(wePOKE)总是真的是有挂,安装教程(有挂规律)1、实时...
透视线上!德朴之星脚本,wpk... 透视线上!德朴之星脚本,wpk辅助器下载,玩家必看科普(有挂规律)1、系统规律教程、辅助透视等服务,...
透视辅助!红龙poker辅助,... 透视辅助!红龙poker辅助,wpk透视工作室,让我来分享经验(有挂技巧)1、下载好辅助软件之后点击...
wepower有外挂!wepo... wepower有外挂!wepoke辅助挂,(Wepoke)竟然是有挂,wpk教程(有挂插件)1、在w...
微扑克辅助机器人!wpk ai... 微扑克辅助机器人!wpk ai辅助,(WEPoke)其实是真的有挂,必备教程(有挂详情)1、实时微扑...
透视神器!hhpoker怎么开... 透视神器!hhpoker怎么开透视,德普之星透视辅助软件下载,热点推荐(有挂技巧)1、很好的工具软件...