Android如何在WebView的外部HTML页面上加载自定义CSS

时间:2012-09-25 04:46:53

标签: android css

我想在HTML页面上应用我的自定义CSS,但是它没有用。

 wv.loadUrl("<style>.featured {"+
        "   background-image: -ms-linear-gradient(bottom, #F9F6F9 0%, #ECE4F4 100%);"+
        "   background-image: -moz-linear-gradient(bottom, #F9F6F9 0%, #ECE4F4 100%);"+
        "   background-image: -o-linear-gradient(bottom, #F9F6F9 0%, #ECE4F4 100%);"+
        "background-image: -webkit-gradient(linear, left bottom, left top, color-stop(0, #F9F6F9), color-stop(1, #ECE4F4));"+
        "   background-image: -webkit-linear-gradient(bottom, #F9F6F9 0%, #ECE4F4 100%);"+
        "   background-image: linear-gradient(to top, #F9F6F9 0%, #ECE4F4 100%);"+
        "}"); 

请帮帮我。

3 个答案:

答案 0 :(得分:1)

感谢您的回答,但我得到了解决方案。

这是我的解决方案。

wv.loadUrl("javascript:(function() { " +                        
    "var divs = document.getElementsByClassName('free');"+
     "for(var i=0; i<divs.length; i++)"+
     "{"+
        "divs[i].style.backgroundImage='-webkit-gradient(linear, left bottom, left top, color-stop(0, #FFFFFF), color-stop(1, #EFD2E4))';"+
      "}"+
     "})()");   

使用此功能,您可以将自定义CSS应用于Android中HTML的任何标记或元素。

答案 1 :(得分:0)

将您的html和css文件放在Assets文件夹中。并使用以下代码。

Main.java

WebView webview = (WebView) findViewById(R.id.abtus_webView);
webview.loadUrl("file:///android_asset/index.html");

如果你想将图像用于html页面,那么将下面的代码添加到html页面。

htmltest.html

<img src="file:///android_asset/images/abc.png">

我已将图像放入Assets文件夹中的图像文件夹中。这对我来说是正确的,我希望它对您有所帮助。

答案 2 :(得分:0)

您可以使用javascript:网址功能注入自定义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());
}

这是上述方法的一个用法示例:

@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; }"
    );
}