Android - after loading URL with webview can i change background color

后端 未结 1 1228
情书的邮戳
情书的邮戳 2021-01-12 16:42

I have a webview and im loading an external HTML form a site. I try to change the background color using javascript function:

    function changeBGC(color)         


        
相关标签:
1条回答
  • 2021-01-12 17:42

    You can run javascript using the WebViewClient, example here.

    The javascript code that changes the background color of a document.

    So to put it all together:

    When initing WebView:

    WebView webview = new WebView();
    webview.setWebViewClient(new WebClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("stackoverflow.com");
    

    Your web view client:

    public class WebClient extends WebViewClient {
    
        int color;
    
        public WebClient(int color) {
            this.color = color;
        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    
        @Override
        public void onPageFinished(WebView view, String url) 
        {
            String command = "javascript:document.body.style.background = " + color + ";";
            view.loadUrl(command);       
        }
    }
    
    0 讨论(0)
提交回复
热议问题