日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 加载显示富文本——TextView显示富文本和WebView显示富文本,WebView显示图片适配屏幕宽度 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?TextView加載顯示

添加依賴

implementation 'com.zzhoujay.richtext:richtext:3.0.8' implementation 'com.zzhoujay:html:1.0.2'

調(diào)用

/*** 加載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.1return context.createConfigurationContext(new Configuration());return context;} }

2、引用

<cn.dlc.bangbang.electricbicycle.util.views.LollipopFixedWebViewandroid:id="@+id/webView"android:layout_width="match_parent"android:layout_height="wrap_content"></cn.dlc.bangbang.electricbicycle.util.views.LollipopFixedWebView>

3、java代碼實現(xiàn)

方法一

String topss="<html><head><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\"\"></head><body><style>img{width:100%%!important;height:auto!important;}p{font-size:14px;}</style>"; LgqLogutil.e("lll000ll=== "+content); String endss="</body></html>";

方法二?

?

String js = "<script type=\"text/javascript\">"+"var imgs = document.getElementsByTagName('img');" + // 找到img標(biāo)簽"for(var i = 0; i<imgs.length; i++){" + // 逐個改變"imgs[i].style.width = '100%';" + // 寬度改為100%"imgs[i].style.height = 'auto';" +"}" +"</script>";

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

webView.loadData((ss+js).replace("&amp;zoom=640w",""), "text/html;charset=utf-8", null); public void initfte(String ss){WebSettings settings = webView.getSettings();// 設(shè)置WebView支持JavaScriptsettings.setJavaScriptEnabled(true);//支持自動適配settings.setUseWideViewPort(true);settings.setLoadWithOverviewMode(true);settings.setSupportZoom(false); //支持放大縮小settings.setBuiltInZoomControls(false); //顯示縮放按鈕settings.setBlockNetworkImage(true);// 把圖片加載放在最后來加載渲染settings.setAllowFileAccess(true); // 允許訪問文件settings.setSaveFormData(true);settings.setGeolocationEnabled(true);settings.setDomStorageEnabled(true);settings.setJavaScriptCanOpenWindowsAutomatically(true);/// 支持通過JS打開新窗口settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);//設(shè)置不讓其跳轉(zhuǎn)瀏覽器webView.setWebViewClient(new WebViewClient() {@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {return false;}});// 添加客戶端支持webView.setWebChromeClient(new WebChromeClient());// mWebView.loadUrl(TEXTURL);//不加這個圖片顯示不出來if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);}webView.getSettings().setBlockNetworkImage(false);//允許cookie 不然有的網(wǎng)站無法登陸CookieManager mCookieManager = CookieManager.getInstance();mCookieManager.setAcceptCookie(true);mCookieManager.setAcceptThirdPartyCookies(webView, true);webView.loadDataWithBaseURL(null,ss, "text/html" , "utf-8", null);//加載html數(shù)據(jù) // webView.loadUrl(URL);}

?

bug在線交流:QQ1085220040

?

總結(jié)

以上是生活随笔為你收集整理的android 加载显示富文本——TextView显示富文本和WebView显示富文本,WebView显示图片适配屏幕宽度的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。