2016-11-13 15 views
3

私のAndroidアプリでカスタムフォントを使用したかったのですが、TextViewのカスタムフォントはうまく動作しますが、WebViewにカスタム(ユニコード)私は可能な限りあらゆる方法で試しましたが失敗しました。私のコードを見て、私が何かを逃したかどうかを見てみましょう。ありがとうございました。!私のAndroidアプリケーション用のWebViewでカスタム(ユニコード)フォントを使用

アセットフォルダにカスタムフォントファミリを含む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は次のようになり

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

のindex.htmlファイルにHTMLコードを置きます。以下のため..
@font-face { 
    font-family: trado; 
    src: url('file:///android_asset/fonts/trado.ttf'); 
} 

はそれが動作を期待..あなたは資産フォルダにフォントファイルを配置する必要があり

<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は次のようになります資産フォルダからCSSファイルを呼び出します完全な例github click

+0

私はhtmlファイルを使いたくない、私はちょうど私のwordpressベースのアンドロイドアプリの投稿にカスタム(ユニコード)フォントを使いたいと思う。 – SNoV

関連する問題