html图片排列布局下面字_解析展示文字图片混排的(不是真正的html格式)

本文介绍了如何在Android中处理服务端返回的富文本,特别是包含图片的情况。通过自定义`HtmlTextView`类,实现了图片与文字的混排。这个类解析非标准HTML文本,并支持从本地或网络加载图片。在布局文件中添加`HtmlTextView`,然后在代码中调用`setHtmlFromString()`方法设置富文本内容。
摘要由CSDN通过智能技术生成

【需求】服务端 返回富文本形式(实则不是真正意义上的html文本)

图片.png

【工具类】

/**

* @Author Lee

* @Time 2018/9/18

* @Theme

*/

public class HtmlTextView extends TextView {

public static final String TAG = "HtmlTextView";

public static final boolean DEBUG = false;

public HtmlTextView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}

public HtmlTextView(Context context, AttributeSet attrs) {

super(context, attrs);

}

public HtmlTextView(Context context) {

super(context);

}

/**

* @param is * @return

*/

static private String convertStreamToString(InputStream is) {

java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");

return s.hasNext() ? s.next() : "";

}

/**

* Parses String containing HTML to Android's Spannable format and displays * it in this TextView. * * @param html String containing HTML, for example: "Hello world!"

*/

public void setHtmlFromString(String html, boolean useLocalDrawables) {

Html.ImageGetter imgGetter = null;

if (useLocalDrawables) {

// imgGetter = new LocalImageGetter(getContext());

} else {

imgGetter = new UrlImageGetter(this, getContext());

}

// this uses Android's Html class for basic parsing, and HtmlTagHandler

setText(Html.fromHtml(html, imgGetter, new Html.TagHandler() {

@Override

public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {

}

}));

// make links work

setMovementMethod(LinkMovementMethod.getInstance());

setTextColor(getResources().getColor(R.color.shell_news_content));

}

}

【使用】

(1)在布局中:

android:id="@+id/htv"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:textColor="@color/shell_font_black"

android:textSize="18sp"/>

(2) 在代码中:

mHtv.setHtmlFromString(“ ”,false);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值