首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在安卓系统中为webView定义样式

在安卓系统中为webView定义样式
EN

Stack Overflow用户
提问于 2021-01-15 18:40:36
回答 1查看 30关注 0票数 0

我想通过使用下面的代码来填充webView:

代码语言:javascript
复制
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文件,如下所示:

代码语言:javascript
复制
#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更改为:

代码语言:javascript
复制
"<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" +

代码语言:javascript
复制
wb_OpenSource.loadDataWithBaseURL( "file://android_asset/cssStyles.css", str, "text/html", "utf-8", null );

但它似乎不起作用。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-15 19:01:26

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

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

代码语言:javascript
复制
webView.loadDataWithBaseURL("file:///android_asset", HTML_STRING, "text/html","utf-8",null);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65734588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档