android webview 图片异步加载,WebView异步加载自定义CSS/JS

本文介绍了如何在Android WebView中实现图片异步加载和动态替换HTML中的CSS文件,通过重写WebViewClient的shouldInterceptRequest方法,实现自定义CSS和JS的加载,以达到定制界面的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

http://ngudream.com/http://ngudream.com/

近来接到一个需求,就是 webview 要动态加载 css 文件,替换 html 文件原来的,达到定制界面的目的。自己做demo 试验证了一下,webview 本身提供这样的接口,可以达到要求。主要就是使用 webview 的 shouldInterceptRequest 接口,拦截请求。

拦截接口

WebView 调用 loadUrl 后,会首先根据传入的URL获取响应,然后再将响应显示到页面上。根据这个原理,我们可以在获取响应过程中重新改变请求URL或者直接将响应替换。要想拦截 WebView 加载网页我们必须重写 WebViewClient 类,在 WebViewClient 类中我们重写 shouldInterceptRequest() 方法。

通过查看 WebViewClient 的源码我们可以发现,有两个同名方法,分别是:/**

* Notify the host application of a resource request and allow the

* application to return the data. If the return value is null, the WebView

* will continue to load the resource as usual. Otherwise, the return

* response and data will be used. NOTE: This method is called on a thread

* other than the UI thread so clients should exercise caution

* when accessing private data or the view system.

*

* @param view The [email protected] android.webkit.WebView} that is requesting the

* resource.

* @param url The raw url of the resource.

* @return A [email protected] android.webkit.WebResourceResponse} containing the

* response information or null if the WebView should load the

* resource itself.

* @deprecated Use [email protected] #shouldInterceptRequest(WebView, WebResourceRequest)

* shouldInterceptRequest(WebView, WebResourceRequest)} instead.

*/

@Deprecated

public WebResourceResponse shouldInterceptRequest(WebView view,

String url) {

return null;

}

为了让每次加载 html 页面都能够回调 shouldInterceptRequest 函数࿰

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值