如何在Android的html页面上添加外部css文件

时间:2012-06-26 09:26:28

标签: android webview android-webview

我正在开发一个包含许多html页面的应用程序。我在Eclipse的raw文件夹中创建了这些。

我将制作许多html文件,并且我希望只有一个位置用于我想在每个html文件中调用的css文件。这个应用程序以这样的方式工作 loadData方法显示html页面,如下所示:

webview.loadData(readTextFromResource(R.raw.gen1), "text/html", "utf-8");

我很欣赏任何想法。

1 个答案:

答案 0 :(得分:5)

您可以复制项目资产中的html和css文件,并将html文件从资产加载到webview,如下代码所示:

    WebView wv;  
    wv = (WebView) findViewById(R.id.webView1);  
    wv.setBackgroundColor(0);
    wv.setBackgroundResource(android.R.color.black);
    wv.setWebChromeClient(new WebChromeClient());
    wv.setWebViewClient(new WebViewClient());
    wv.getSettings().setJavaScriptEnabled(true);
    wv.getSettings().setPluginsEnabled(true);
    wv.loadUrl("file:///android_asset/Index_Animation/test.html");

如下面的屏幕截图:

enter image description here

并在下面的html文件中引用css:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="./style.css" type="text/css" media="screen"><!--This line refer the css file-->

  </head>
    <body bgcolor="#000000">        

                <div class="col_left">
                    <div class="logo">
                        <div class="circles">
                            <div class="circle"></div>
                            <div class="circle1"></div>
                            <div class="circle2"></div>
                        </div>
                        <center>
                        <img src="./Untitled.png" />
                        </center>
                    </div>

                </div>

</body></html>