2013-10-17 97 views
0

我有一個android webview,其中的文字有sans-serif字體。我有資產的ttf文件/ SANS-serif.ttf 下面是代碼webview字體沒有得到應用

WebView webVw; 
String webContent; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     webVw=(WebView)findViewById(R.id.webVw); 
     webContent="%3Cdiv%20style%3D%22width%3A100%25%3Bheight%3A100%25%3Bposition%3Arelative%3Bleft%3A.1%25%3Btop%3A.1%25%22%3E%3Cdiv%20style%3D%22position%3Aabsolute%3Bwidth%3A31%25%3Bheight%3A42.22222222222222%25%3Bleft%3A36.375%25%3Btop%3A19.02313624678663%25%3Bcolor%3Argb(0%2C%200%2C%200)%3Bfont-size%3A50px%3Bfont-style%3Anormal%3Bfont-family%3Asans-serif%2C%20sans-serif%2C%20sans-serif%3Bfont-weight%3A400%22%20%3EOverlay!%20%3C%2Fdiv%3E%3Ca%20href%3D%22javascript%3A%20void(0)%22%20%20target%3D%22_blank%22%20%3E%3Cimg%20src%3D%22https%3A%2F%2Fsi0.twimg.com%2Fprofile_images%2F1490344068%2FLOGO_COGNIZANT_C_only_normal.jpg%22%20style%3D%22position%3Aabsolute%3Bwidth%3A4.875%25%3Bheight%3A10.444444444444445%25%3Bleft%3A0.625%25%3Btop%3A1.2853470437017995%25%22%20%2F%3E%3C%2Fa%3E%3Cdiv%20style%3D%22position%3Aabsolute%3Bwidth%3A4%25%3Bheight%3A6.666666666666667%25%3Bleft%3A95%25%3Btop%3A1.5424164524421593%25%3Bbackground%3Argb(47%2C%206%2C%20246)%20none%22%20%3E%3C%2Fdiv%3E%3Cdiv%20class%3D%22cf-component%20buttonwrap%22%20style%3D%22position%3Aabsolute%3Bborder%3Dradius%3A5px%3Bwidth%3A4.25%25%3Bheight%3A4.222222222222222%25%3Bleft%3A6.125%25%3Btop%3A3.3419023136246784%25%3Bbackground%3Argb(6%2C%20115%2C%20233)%3Bfont-size%3A15px%3Bfont-style%3Anormal%3Bfont-family%3Asans-serif%2C%20sans-serif%2C%20sans-serif%3Bfont-weight%3A400%22%20%3E%3Ca%20%20class%3D%22editableTxt%22%20href%3D%22http%3A%2F%2Fwww.cognizant.com%22%20target%3D%22_blank%22%3E%20Go!%3C%2Fa%3E%3C%2Fdiv%3E%3C%2Fdiv%3E"; 
     String head="<head><style>@font-face {font-family: 'sans-serif';src: url('file:///assets/sans-serif.ttf');}body {font-family: 'sans-serif';}</style></head>"; 
     String HtmlString = "<html>"+head+"<body>"+webContent+"</body></html>"; 
    webVw.loadData(HtmlString, "text/html","charset=UTF-8"); 

    } 

web視圖顯示正常的字體,但不是無襯線

+0

我也遇到了與webview相同的問題,webview沒有采取自定義字體,但相同的字體應用於其他控件。我不知道爲什麼webview不應用自定義字體,我也加載html到webview – Kasma

回答

1

嘗試......

在您的資產/字體文件夾,放置所需的OTF或TTF字體(此處爲MyFont.otf) 創建一個HTML文件,您將用於資產文件夾內的WebView內容(這裏位於assets/demo/my_page.html中):

<html> 
<head> 
<style type="text/css"> 
@font-face { 
    font-family: MyFont; 
    src: url("file:///android_asset/fonts/MyFont.otf") 
} 
body { 
    font-family: MyFont; 
    font-size: medium; 
    text-align: justify; 
} 
</style> 
</head> 
<body> 
Your text can go here! Your text can go here! Your text can go here! 
</body> 
</html> 

加載HTML到的WebView從代碼:

webview.loadUrl("file:///android_asset/demo/my_page.html"); 

注意到,通過注射loadData()的HTML對我來說沒有奏效。我很想知道是否有人能夠做到這一點。

0

使用此代碼,而不是它

webVw.loadDataWithBaseURL("file:///android_asset/",HtmlString, "text/html", "UTF-8",null); 
0

在某些情況下,如果你嘗試過很多辦法來解決這個問題,你沒有得到你的問題還沒有得到解決。你應該嘗試檢查你的字體文件的正確名稱。打開你的字體文件並檢查字體名稱,將其複製並重命名爲正確的名稱。 在你的html代碼中也使用正確的名字。 我有這個問題幾次!