您的当前位置:首页正文

android 加载显示富文本——TextView显示富文本和WebView显示富文本,WebView显示图片适配屏幕宽度

来源:九壹网

添加依赖

implementation ‘com.zzhoujay.richtext:richtext:3.0.8’

implementation ‘com.zzhoujay:html:1.0.2’

调用

/**

  • 加载Html

  • @param html

*/

protected void loadHtml(String html, TextView textContent) {

try {

RichText.fromHtml(html).into(textContent);

} catch (Exception e) {

e.printStackTrace();

}

}

WebView加载显示

============

1、封装webview

public class LollipopFixedWebView extends WebView {

public LollipopFixedWebView(Context context) {

super(getFixedContext(context));

}

public LollipopFixedWebView(Context context, AttributeSet attrs) {

super(getFixedContext(context), attrs);

}

public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr) {

super(getFixedContext(context), attrs, defStyleAttr);

}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

super(getFixedContext(context), attrs, defStyleAttr, defStyleRes);

}

public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) {

super(getFixedContext(context), attrs, defStyleAttr, privateBrowsing);

}

public static Context getFixedContext(Context context) {

if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 23) // Android Lollipop 5.0 & 5.1

return context.createConfigurationContext(new Configuration());

return context;

}

}

2、引用

<cn.dlc.bangbang.electricbicycle.util.views.LollipopFixedWebView

android:id=“@+id/webView”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”></cn.dlc.bangbang.electricbicycle.util.views.LollipopFixedWebView>

3、java代码实现

方法一

String topss=“<meta charset=”“UTF-8"”><meta name=““viewport”” content=““width=device-width, initial-scale=1.0,user-scalable=no””><meta http-equiv=““X-UA-Compatible”” content=““ie=edge””>";

LgqLogutil.e("lll000ll=== "+content);

String endss=“”;

方法二

String js = “<script type=\“text/javascript\”>”+

“var imgs = document.getElementsByTagName(‘img’);” + // 找到img标签

“for(var i = 0; i<imgs.length; i++){” + // 逐个改变

“imgs[i].style.width = ‘100%’;” + // 宽度改为100%

“imgs[i].style.height = ‘auto’;” +

“}” +

“”;

webView.loadData((data.getProjectDescription()+js).replace(“&zoom=640w”,“”), “text/html;charset=utf-8”, null);

webView.loadData((ss+js).replace(“&zoom=640w”,“”), “text/html;charset=utf-8”, null);

结尾

我还总结出了互联网公司Android程序员面试涉及到的绝大部分面试题及答案,并整理做成了文档,以及系统的进阶学习视频资料分享给大家。
(包括Java在Android开发中应用、APP框架知识体系、高级UI、全方位性能调优,NDK开发,音视频技术,人工智能技术,跨平台技术等技术资料),希望能帮助到你面试前的复习,且找到一个好的工作,也节省大家在网上搜索资料的时间来学习。

参考docs.qq.com/doc/DSkNLaERkbnFoS0ZF
给大家。
(包括Java在Android开发中应用、APP框架知识体系、高级UI、全方位性能调优,NDK开发,音视频技术,人工智能技术,跨平台技术等技术资料),希望能帮助到你面试前的复习,且找到一个好的工作,也节省大家在网上搜索资料的时间来学习。**

[外链图片转存中…(img-CalYkWow-1724309351714)]

参考docs.qq.com/doc/DSkNLaERkbnFoS0ZF

因篇幅问题不能全部显示,请点此查看更多更全内容

Top