如何使我的.css和.js文件与我在Android上的WebView中加载的html代码一起使用

时间:2016-08-04 00:52:23

标签: javascript android html css webview

所以我在html中创建了一个简单的待办事项列表,并且我与.html文件关联的.css和.js文件在浏览器中正常工作。现在我试图让它作为一个Android应用程序在Web视图中工作。我正在使用AndroidStudio并创建了一个资源文件夹,并将我的.css和.js文件添加到了它们中。我将所有的html代码复制到一个字符串中,并使用loadWithBaseURL。只有我的html在Web视图中加载,没有任何样式或功能。有人能指出我正确的方向让这个工作吗?

enter image description here

package com.android.todowebview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.app.Activity;

public class MainActivity extends Activity {

    private WebView browser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        browser = (WebView) findViewById(R.id.webView1);
        browser.getSettings().setJavaScriptEnabled(true);

        String todoHtml = "<html>\n" +
                "  <head>\n" +
                "    <title>ToDoList</title>\n" +
                "    <meta http-equiv=\"Content-Type\" content=\"text/html\" charset=\"UTF-8\"/>\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\">\n" +
                "    <meta name=\"apple-mobile-web-app-cabable\" content=\"yes\"/>\n" +
                "    <link rel=\"stylesheet\" href=\"styles.css\"\n" +
                "  </head>\n" +
                "  <body>\n" +
                "    <head>\n" +
                "      <h1>\n" +
                "        ToDo-List\n" +
                "      </h1>\n" +
                "    </head>\n" +
                "    <form>\n" +
                "      <label>\n" +
                "        Title:\n" +
                "        <input id=\"newitem\" type=\"text\" placeholder=\"Tap to enter a new item&hellip;\">\n" +
                "        <br>\n" +
                "        Details:\n" +
                "        <input id=\"detail\" type=\"text\" placeholder=\"Details of new item&hellip;\">\n" +
                "        <br>\n" +
                "        Date:\n" +
                "        <input id=\"date\" type=\"date\" placeholder=\"year/month/day\">\n" +
                "        <br>\n" +
                "        Time:\n" +
                "        <input id=\"time\" type=\"time\" placeholder=\"00:00\">\n" +
                "        <br>\n" +
                "      </label>\n" +
                "      <button type=\"button\" id=\"additem\">\n" +
                "        Add\n" +
                "      </button>\n" +
                "    </form>\n" +
                "    <ul id=\"todo-list-container\">\n" +
                "    </ul>\n" +
                "    <!--\n" +
                "    <p id=\"total\">\n" +
                "      <span class=\"total1\">\n" +
                "        Total:\n" +
                "        <span id=\"totalcount\">\n" +
                "          0\n" +
                "        </span>\n" +
                "      </span>\n" +
                "      <span class=\"remain\">\n" +
                "        Remaining:\n" +
                "        <span id=\"remaining\">\n" +
                "          0\n" +
                "        </span>\n" +
                "      </span>\n" +
                "    </p>\n" +
                "    -->\n" +
                "    <fieldset>\n" +
                "      <button class=\"button deletechecked\" type=\"button\" id=\"delete-checked-items\">\n" +
                "        Delete Checked\n" +
                "      </button>\n" +
                "      <button class=\"button deleteall\" type=\"button\" id=\"delete-all-items\">\n" +
                "        Delete All\n" +
                "      </button>\n" +
                "    </fieldset>\n" +
                "\n" +
                "    <script type=\"text/template\" id=\"item-template\">\n" +
                "    <li>\n" +
                "      <label>\n" +
                "        <input type=\"checkbox\">\n" +
                "        <!-- ITEM_NAME -->\n" +
                "      </label>\n" +
                "    </li>\n" +
                "    </script>\n" +
                "    \n" +
                "    <script src=\"todo_back.js\">\n" +
                "    </script>\n" +
                "  </body>\n" +
                "</html>";

        //browser.loadData(todoHtml, "text/html", "UTF-8");
        browser.loadDataWithBaseURL("file:///android_asset", todoHtml, "text/html", "UTF-8", null);

    }
}

1 个答案:

答案 0 :(得分:0)

出于测试目的,或快速而肮脏的方法,您可以尝试将css文件内容移动到样式标记内,即脚本中的js 1,以避免链接/文件夹错误。