Changing text color in a WebView?

后端 未结 8 441
梦如初夏
梦如初夏 2020-12-17 09:06

There\'s a method for altering background color but not font.
Any ideas?

相关标签:
8条回答
  • 2020-12-17 09:24

    This worked for me

    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url) {
        view.loadUrl(
            "javascript:document.body.style.setProperty(\"color\", \"white\");"
        );
      }
    });
    
    0 讨论(0)
  • 2020-12-17 09:27

    I'm not sure I understand. The WebView just displays the HTML you give it so you would just use normal HTML/CSS to modify the content displayed within.

    0 讨论(0)
  • 2020-12-17 09:33

    This is the easiest way I found (change the text color to white for example):

    webview.loadUrl("javascript:document.body.style.color=\"white\";");
    
    0 讨论(0)
  • 2020-12-17 09:36

    something like

    String text = "<html><head>"
              + "<style type=\"text/css\">body{color: #fff; background-color: #000;}"
              + "</style></head>"
              + "<body>"                          
              + your_string_text_here
              + "</body></html>";
    
    webview1.loadData(text, "text/html", "utf-8");
    
    0 讨论(0)
  • 2020-12-17 09:36

    I had to put it in the onPageFinished method.

    _webView.setWebViewClient(new WebViewClient() {
        public void onPageFinished(WebView view, String url) {
            _webView.loadUrl(
                "javascript:document.body.style.setProperty(\"color\", \"white\");"
            );
        }
    });
    
    0 讨论(0)
  • 2020-12-17 09:36

    When the buffer is SPANNABLE, modifying the HTML directly is an ideal solution. The font, color, typeface, style can all be affected through HTML:

    String szMessage = "<font face='trebuchet' size=30><a href=zz><b>click me</b></a></font>";
    
    TextView tv = (TextView)findViewById(R.id.tv_message);
    tv.setText(Html.fromHtml(szMessage), BufferType.SPANNABLE);
    
    0 讨论(0)
提交回复
热议问题