【问题标题】:Define style to HTML for webView in Android在 Android 中为 webView 定义 HTML 样式
【发布时间】:2021-01-15 21:02:34
【问题描述】:

我想通过以下方式使用 HTML 填充 webView:

String str = "<html>" +
        "<body>" +
        "<style> \n" +
            "h3 {\"font-family:helvetica,arial,sans-serif;font-size:24px;line-height:1.3;\"} \n" +
            "p {\"font-family:helvetica,arial,sans-serif;font-size:12px;line-height:1.3;\"} \n" +
        "</style> \n" +
        "<h3> Open-Source licenses</h3>\n" +
        "<p> A huge thanks from all of us at to the great people behind the great programs listed below.</p>\n" +
        "<ul>\n" +
        "<li>Asynchronous Http Client for Android</li>\n" +
        "<li>Glide</li>\n" +
        "<li>Google Play Service</li>\n" +
        "<li>Gson</li>\n" +
        "<li>Picasso</li>\n" +
        "</ul>\n" +
        "<hr>" +
        "<h3>Asynchronous Http Client for Android</h3>\n" +
        "<p><a href=\"https://github.com/android-async-http/android-async-http\">https://github.com/android-async-http/android-async-http</a></p> \n" +
        "<p>Unless required by applicable law or agreed to in writing, software<br>distributed under the License is distributed on an &quot;AS IS&quot; BASIS,<br>WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<br>See the License for the specific language governing permissions and<br>limitations under the License.</p>\n" +
        "</body></html>";

wb_OpenSource.loadDataWithBaseURL( null, str, "text/html", "utf-8", null );

但是,风格似乎没有任何区别。

有什么方法可以让它工作而不是为每一行添加样式?

更新:

如下所示,我创建了一个如下所示的 .css 文件:

#topHeader {
    font-family:helvetica,arial,sans-serif;font-size:24px;line-height:1.3;
}

#regHeader {
    font-family:helvetica,arial,sans-serif;font-size:20px;line-height:1.3;
}

#topText {
    font-family:helvetica,arial,sans-serif;font-size:14px;line-height:1.3;
}

#regText {
    font-family:helvetica,arial,sans-serif;font-size:12px;line-height:1.3;
}

#links {
    font-family:helvetica,arial,sans-serif;font-size:16px;line-height:1.3;
}

并将我的 HTML 更改为:

"<body>" +
"<link rel=\"stylesheet\" href=\"cssStyles.css\"> \n" +
"<h3 id=\"topHeader\"> Open-Source licenses</h3>\n" +
"<p id=\"topText\"> A huge thanks from all of us at to the great people behind the great programs listed below</p>\n" +
"<ul>\n" +
"<li id=\"regText\"> Asynchronous Http Client for Android </li>\n" +
"<li id=\"regText\"> ExpandableTextView Android Arsenal </li>\n" +

wb_OpenSource.loadDataWithBaseURL( "file://android_asset/cssStyles.css", str, "text/html", "utf-8", null );

但它似乎不起作用。

谢谢

【问题讨论】:

    标签: java android html android-webview


    【解决方案1】:

    您可以创建一个单独的.css 文件来设置元素的样式。将 .css 文件放在 assets 文件夹中,并将其包含在您的 html 代码中。

    现在在 webview 中加载 HTML,如下所示:

    webView.loadDataWithBaseURL("file:///android_asset", HTML_STRING, "text/html","utf-8",null);
    

    【讨论】:

    • 能否提供更多细节?我创建了一个 .css 文件并将其放在资产中。我是否需要更改我的 HTML_STRING 并在其中放置指向 .css 的链接,或者因为我在 file:///android_asset 中提到它已经包含它?因为现在它不适合我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 2021-08-05
    相关资源
    最近更新 更多