2016-11-13 65 views
3

我想在我的Android應用程序中使用自定義字體,TextView中的自定義字體正常工作,但我找不到在WebView中添加自定義(unicode)字體的方法,我儘可能地嘗試過,但失敗了,看看我的代碼,看看我是否錯過了一些東西。謝謝。!在WebView中爲我的Android應用程序使用自定義(unicode)字體

我在資產文件夾中創建了一個帶有自定義字體系列的CSS文件,並用LoadDataWithBaseURL調用它,但它不起作用。

 htmlTextView = (WebView) findViewById(R.id.htmlTextView); 

     String text = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />"; 
     htmlTextView.loadDataWithBaseURL("file:///android_asset/", text, "text/html", "UTF-8", null); 

     htmlTextView.getSettings().setJavaScriptEnabled(true); 
     htmlTextView.setBackgroundColor(Color.TRANSPARENT); 
     htmlTextView.getSettings().setDefaultFontSize(
       WebHelper.getWebViewFontSize(this)); 

CSS文件:

@font-face { 
    font-family: 'iskoola pota'; 
    src: url('fonts/iskpota.ttf') format('truetype'); 
} 

回答

0

有一種方法把你的HTML代碼中的index.html文件

webView.loadUrl("file:///android_asset/html/index.html"); 

HTML這個樣子。從資產文件夾調用CSS文件

<html> 
<head><LINK href="file:///android_asset/css/myStyle.css" type="text/css" rel="stylesheet"/></head> 
<body> 
    <img class="img" src="file:///android_asset/img/ok.png"> 
    <p > This above image is shown from assets folder... </p> 
    <p>Custom font <span style="color:red">'trado'</span> from asset folder has been applied on this text</p> 
</body> 

和CSS看起來就像這樣,你需要把資產文件夾中的字體文件..

@font-face { 
    font-family: trado; 
    src: url('file:///android_asset/fonts/trado.ttf'); 
} 

希望工程。對於github上的完整示例click

+0

我不想使用html文件,我只是想在我的wordpress基於android應用程序中使用自定義(unicode)字體。 – SNoV

相關問題