Android 更改 WebView 中的文本颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1254970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing text color in a WebView?
提问by yanchenko
There's a method for altering background color but not font.
Any ideas?
有一种方法可以改变背景颜色而不是字体。
有任何想法吗?
采纳答案by MattC
I'm not sure I understand. The WebView just displays the HTML you give it so you would just use normal HTML/CSS to modify the content displayed within.
我不确定我是否理解。WebView 只显示您提供的 HTML,因此您只需使用普通的 HTML/CSS 来修改其中显示的内容。
回答by OWADVL
something like
就像是
String text = "<html><head>"
+ "<style type=\"text/css\">body{color: #fff; background-color: #000;}"
+ "</style></head>"
+ "<body>"
+ your_string_text_here
+ "</body></html>";
webview1.loadData(text, "text/html", "utf-8");
回答by djunod
I had to put it in the onPageFinished method.
我不得不把它放在 onPageFinished 方法中。
_webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
_webView.loadUrl(
"javascript:document.body.style.setProperty(\"color\", \"white\");"
);
}
});
回答by Ismail Iqbal
This worked for me
这对我有用
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
view.loadUrl(
"javascript:document.body.style.setProperty(\"color\", \"white\");"
);
}
});
回答by RefaelJan
This is the easiest way I found (change the text color to white for example):
这是我找到的最简单的方法(例如将文本颜色更改为白色):
webview.loadUrl("javascript:document.body.style.color=\"white\";");
回答by Jess Smith
@rafraph's answer didn't work for me. I had to use
@rafraph 的回答对我不起作用。我不得不使用
webView.loadUrl("javascript:document.body.style.setProperty(\"color\", \"white\");");
回答by paiego
When the buffer is SPANNABLE, modifying the HTML directly is an ideal solution. The font, color, typeface, style can all be affected through HTML:
当缓冲区为 SPANNABLE 时,直接修改 HTML 是一个理想的解决方案。字体、颜色、字体、样式都可以通过 HTML 来影响:
String szMessage = "<font face='trebuchet' size=30><a href=zz><b>click me</b></a></font>";
TextView tv = (TextView)findViewById(R.id.tv_message);
tv.setText(Html.fromHtml(szMessage), BufferType.SPANNABLE);
回答by Vladimir Salguero
You can concatenate your answer one body tag HTML with CSS style hex color, this is an example using a JSON response
您可以将您的答案与 CSS 样式十六进制颜色连接到一个正文标记 HTML,这是使用 JSON 响应的示例
First: function for decode JSON to HTML format
第一:将 JSON 解码为 HTML 格式的函数
public String stripHtml(String html) {
return Html.fromHtml(html).toString();
}
Second: Load data in WebView (no url)
二:在WebView中加载数据(无url)
String string_html;
string_html = "<body style="color:#535362;">" + youStringHTML + "</body>";
webView.loadDataWithBaseURL(null, stripHtml(string_html), "text/html", "utf-8", null);