简体   繁体   English

如何在android中设置webview的文本颜色

[英]how set text color of webview in android

i am trying to change text color of webview with this code 我正在尝试使用此代码更改webview的文本颜色

String message ="<font color='white'>"+"<u>"+
"text in white"+ "<br>" +
"<font color='cyan'>"+"<font size='2'>"+
" text in blue color "+"</font>";
webview.loadData(message, "text/html", "utf8"); 

but i have some html pages. 但我有一些HTML页面。 store in my sdcard then how can i change text color.. 存储在我的SD卡中然后如何更改文本颜色..

i use 我用

webViewRead.loadUrl(url);

url is path of my file. url是我文件的路径。

You have to give the path of that file like this. 你必须像这样给出该文件的路径。

String extStorageDirectory = Environment.getExternalStorageDirectory()
                .toString() + "/folder_name";

File directory = new File(extStorageDirectory);
File fileInDirectory = new File(directory,file_name.html);

//Read text from file
StringBuilder html_text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(fileInDirectory));
    String line;

    while ((line = br.readLine()) != null) {
        html_text.append(line);
        html_text.append('\n');
    }
}
catch (IOException e) {
    //You'll need to add proper error handling here
}

then use this html code for edit 然后使用此HTML代码进行编辑

String message ="<font color='white'>"+"<u>"+"text in white"+ "<br>" +"<font color='cyan'>"+"<font size='2'>"+" text in blue color "+"</font>"; 
 webview.loadData(message, "text/html", "utf8"); 
htmlDetail = dbValues.getContent(3);
        tvDescription3.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        String finalHtml = "<html><head>"
                  + "<style type=\"text/css\">li{color: #00f} span {color: #000}"
                  + "</style></head>"
                  + "<body>"                          
                  + htmlDetail
                  + "</body></html>";

    tvDescription3.loadData(finalHtml, "text/html; charset=UTF-8", null);

put your file path as 把你的文件路径

String htmlPath = "file:///mnt/sdcard/test/11.html"; 
String baseUrl = "file:///mnt/sdcard/test/"; 
webView.loadDataWithBaseURL(baseUrl, message, "text/html", "utf-8", null); 
webView.loadUrl(htmlPath); 

In order to change a WebView's background color there is a standard way: 为了更改WebView的背景颜色,有一种标准方式:

mWebView.setBackgroundColor(Color.Black);

In order to change a WebView's text font color there is no standard way: Either you change the font through the html code, or you do this: 为了更改WebView的文本字体颜色,没有标准方法:您可以通过html代码更改字体,或者执行以下操作:

htmlData="<font color='black'>" + htmlData + "</font>";
mWebView.loadDataWithBaseURL(null, htmlData, "text/html", "UTF-8", null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM