how to load android assets css files to android WebView URL

后端 未结 2 600
感动是毒
感动是毒 2021-01-12 12:38

Actually I want to load complete site from internet link but i want to load css files from mobile (means not from internet). Because I want to load all page faster using int

相关标签:
2条回答
  • 2021-01-12 13:17

    Please put CSS in assets folder, and refer to CSS by relative path, and load HTML to WebView by loadDataWithBaseURL() method: if your css file name is mycss.css

    StringBuilder data = new StringBuilder();
    data .append("<HTML><HEAD><LINK href=\"mycss.css\" type=\"text/css\" rel=\"stylesheet\"/></HEAD><body>");
    data .append(tables.toString());
    data .append("</body></HTML>");
    webView.loadDataWithBaseURL("file:///android_asset/", data .toString(), "text/html", "utf-8", null);
    

    thank you

    0 讨论(0)
  • 2021-01-12 13:23

    Because the two other answers are just copy pastas and I happened to have the same issue I will help you with my solution:

    You need to specify the file path in the head section of your html file. Pay attention to the part after href

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <link rel="stylesheet" href="file:///android_asset/bootstrap.min.css" />
            <link rel="stylesheet" href="file:///android_asset/font-awesome.min.css" />
        </head>
    </html>
    

    I just can't get my head around why the other posters just copied and pasted and didn't even read what you were trying to tell them.

    I hope this helps.

    Be aware that your resources folder can be named differently.

    0 讨论(0)
提交回复
热议问题