【问题标题】:how set text color of webview in android如何在android中设置webview的文本颜色
【发布时间】:2018-07-10 01:09:51
【问题描述】:

我正在尝试使用此代码更改 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"); 

但我有一些 html 页面。存储在我的 sdcard 中,然后如何更改文本颜色..

我用

webViewRead.loadUrl(url);

url 是我的文件的路径。

【问题讨论】:

标签: android webview


【解决方案1】:

你必须像这样给出那个文件的路径。

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
}

然后使用此 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"); 

【讨论】:

    【解决方案2】:
    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);
    

    【讨论】:

      【解决方案3】:

      把你的文件路径写成

      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); 
      

      【讨论】:

        【解决方案4】:

        为了改变 WebView 的背景颜色,有一个标准的方法:

        mWebView.setBackgroundColor(Color.Black);
        

        为了改变 WebView 的文本字体颜色,没有标准的方法: 要么通过 html 代码更改字体,要么这样做:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-10-04
          • 1970-01-01
          • 2012-09-13
          • 1970-01-01
          • 2023-03-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多