2012-06-27 27 views
3

我試圖改變的WebView的文字顏色與此代碼的WebView的集文本顏色android的

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頁面。店我的SD卡我又如何能更改文本顏色..

我使用

webViewRead.loadUrl(url); 

網址是我的文件的路徑。

+0

請參閱本http://stackoverflow.com/questions/3624171/how顯示html的內容到webview使用安卓 –

+0

但我想顯示的HTML頁面不text.wv.loadDataWithBaseURL(「」,HTML,MIME類型,編碼,「」); 在這個方法中,我應該把我的路徑(文件)PLZ dis-crib。 – Youddh

+0

如果你想加載HTML文件,然後把HTML文件放入資產文件夾。請參考這個鏈接 http://stackoverflow.com/a/10844221/1263679 –

回答

9

你必須給這個文件的路徑像這樣。

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"); 
+0

好的,但我把我的HTML頁面。 – Youddh

1

把你的文件路徑爲

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