1

我想在我尝试过这种方式的 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%);"+
        "}"); 

请帮帮我。

4

3 回答 3

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 的任何标签或元素。

于 2012-09-25T06:20:18.000 回答
0

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

主.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 文件夹中的图像文件夹中。这对我有用,希望对您有所帮助。

于 2012-09-25T05:12:42.250 回答
0

您可以使用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());
}

这是上述方法的使用示例:

@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; }"
    );
}
于 2015-05-16T01:12:30.287 回答