动态地改变网页视图CSS值不重装(Android版) [英] Dynamically changing css values in a webview without reloading (Android)

查看:199
本文介绍了动态地改变网页视图CSS值不重装(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于WebView中应用,我想补充一个夜间模式。
我的第一反应是,只是让程序呈现每个时间视夜间模式是否是或不是正确的CSS值,它的伟大工程,但它意味着页面有每一个模式被触发时加载。
有没有什么办法可以加装主动改变CSS值,而无需重新加载页面?

I have a WebView based app to which I want to add a 'night-mode'. My first instinct was to just have the program render the right CSS values each time depending on whether night-mode was on or not, and it works great, but it means that the page has to be reloaded each time the mode is toggled. Is there any way I can change the css values retro-actively without having to reload the page?

推荐答案

您可以注入code到你的页面的DOM。所以,你可以例如具有复位样式表中,然后注入与JavaScript一天或夜间模式类到DOM的要求是这样的:

You could inject code into your page's DOM. So you could for example have a reset stylesheet, and then inject a day or night mode class with javascript into the dom as required like this:

public class WebClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) 
    {       
        view.loadUrl("javascript:document.getElementById(id).style.property=new style");       
    }
}

心连心

这篇关于动态地改变网页视图CSS值不重装(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆