Android-如何在webview中将自定义css注入外部网页

Android-如何在webview中将自定义css注入外部网页,android,Android,我想将自定义CSS应用到WebView中的外部网页,如下所示 webView.loadUrl("<style>body {font-size:15px;}</style>"); webView.loadUrl("http://www.stackoverflow.com"); webView.loadUrl(“body{font size:15px;}”); webView.loadUrl(“http://www.stackoverflow.com"); 字符串html=

我想将自定义CSS应用到WebView中的外部网页,如下所示

webView.loadUrl("<style>body {font-size:15px;}</style>");
webView.loadUrl("http://www.stackoverflow.com");
webView.loadUrl(“body{font size:15px;}”);
webView.loadUrl(“http://www.stackoverflow.com");
字符串html=…//手动加载
//将样式插入到
loadData(html,“text/html”,空);

您可以使用
javascript:
URL功能注入自定义JS

以下是如何使用Java中的以下命令添加CSS规则:

/**
 * Creates a CSS element in the <head> section of the Web page and assigns it
 * to a `customSheet` JS variable
 */
private final static String CREATE_CUSTOM_SHEET =
    "if (typeof(document.head) != 'undefined' && typeof(customSheet) == 'undefined') {"
        + "var customSheet = (function() {"
            + "var style = document.createElement(\"style\");"
            + "style.appendChild(document.createTextNode(\"\"));"
            + "document.head.appendChild(style);"
            + "return style.sheet;"
        + "})();"
    + "}";

/**
 * Adds CSS properties to the loaded Web page. A <head> section should exist when this method is called.
 * The Web view should be configured with `.getSettings().setJavaScriptEnabled(true);`
 *
 * @param webView Web view to inject into
 * @param cssRules CSS rules to inject
 */
void injectCssIntoWebView(WebView webView, String... cssRules) {
    StringBuilder jsUrl = new StringBuilder("javascript:");
    jsUrl
        .append(CREATE_CUSTOM_SHEET)
        .append("if (typeof(customSheet) != 'undefined') {");
    int cnt = 0;
    for (String cssRule : cssRules) {
        jsUrl
            .append("customSheet.insertRule('")
            .append(cssRule)
            .append("', ")
            .append(cnt++)
            .append(");");
    }
    jsUrl.append("}");

    webView.loadUrl(jsUrl.toString());
}

你能详细说明一下吗?@A droid例如:String html=download(“);html=html.replaceFirst(“,”body{font size:15px;}”);webview.loadData(html,“text/html”,null);
/**
 * Creates a CSS element in the <head> section of the Web page and assigns it
 * to a `customSheet` JS variable
 */
private final static String CREATE_CUSTOM_SHEET =
    "if (typeof(document.head) != 'undefined' && typeof(customSheet) == 'undefined') {"
        + "var customSheet = (function() {"
            + "var style = document.createElement(\"style\");"
            + "style.appendChild(document.createTextNode(\"\"));"
            + "document.head.appendChild(style);"
            + "return style.sheet;"
        + "})();"
    + "}";

/**
 * Adds CSS properties to the loaded Web page. A <head> section should exist when this method is called.
 * The Web view should be configured with `.getSettings().setJavaScriptEnabled(true);`
 *
 * @param webView Web view to inject into
 * @param cssRules CSS rules to inject
 */
void injectCssIntoWebView(WebView webView, String... cssRules) {
    StringBuilder jsUrl = new StringBuilder("javascript:");
    jsUrl
        .append(CREATE_CUSTOM_SHEET)
        .append("if (typeof(customSheet) != 'undefined') {");
    int cnt = 0;
    for (String cssRule : cssRules) {
        jsUrl
            .append("customSheet.insertRule('")
            .append(cssRule)
            .append("', ")
            .append(cnt++)
            .append(");");
    }
    jsUrl.append("}");

    webView.loadUrl(jsUrl.toString());
}
@Override
public void onPageFinished(WebView webView, String url) {
    // Several people probably worked hard on the design of this Web page, let's hope they won't see what's next
    injectCssIntoWebView(
        webView,
        "div { border: 4px solid yellow; }",
        "p { border: 4px solid green; }",
        "a { border: 4px solid black; }",
        "img { border: 4px solid blue; }"
    );
}