本文介绍了如何在Android WebView中设置backGround颜色黑色和文本白色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
package com.example.webviewtheme;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView webview1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview1=(WebView)findViewById(R.id.webView1);
String rawHTML = "<HTML>"+
"<body style='color: #000000; background-color: #ffffff'><h1>Hello Android </h1></body>"+
"</HTML>";
webview1.setBackgroundColor(00000000);
webview1.loadData(rawHTML, "text/HTML", "UTF-8");
}
}
这是我的代码,我想设置WebView的背景色黑色和白色,我尝试在HTML中应用,但无法退出,请帮助我如何在Webview文本和backGround中设置夜间模式
This is My code i want to set background color black and text white of webView i have tried to apply In Html But Unable to get Out put please help me how to set Night Mode in Webview text and backGround
推荐答案
使用此
webview1=(WebView)findViewById(R.id.webView1);
String rawHTML = "<HTML>"+
"<head>"+"<style type=\"text/css\">"+
"body,h1{color: #000000;"+
"background-color: #ffffff;}"+
"</style></head>"+
"<body><h1>Hello Android </h1></body>"+
"</HTML>";
webview1.loadData(rawHTML, "text/html; charset=UTF-8",null);
6月20日更新:
如果要在加载完成后更改网页CSS(例如,当使用 loadUrl
加载webPageAddress时),可以这样操作:
if you want to change web page css after it's load finished (for example when load webPageAddress with loadUrl
) you can do it like this:
-
在
webView
webView.getSettings().setJavaScriptEnabled(true);
在页面加载完成时设置侦听器
set listener on page load finish
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
// Inject CSS on PageFinished
injectCSS();
super.onPageFinished(view, url);
}
});
注入CSS
inject CSS
private void injectCSS() {
webView.loadUrl(
"javascript:document.body.style.setProperty(\"color\", \"white\");"
);
webView.loadUrl(
"javascript:document.body.style.setProperty(\"background-color\", \"black\");"
);
}
这篇关于如何在Android WebView中设置backGround颜色黑色和文本白色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!