2013-07-16 232 views
0

我想开发一个简单的android web浏览器应用程序,它支持我提供的Unicode字体。我做了一些研究,但我找不到一个成功的方法来为android WebView添加自定义字体。有一些解决方案为WebView添加自定义字体,但它们都基于在CSS中添加字体。但由于我无法控制WebView内部加载的页面,因此有一种方法可以直接将字体添加到WebView,如setTypaFace()方法TextView为android webview添加自定义字体

这里是到目前为止,我已经用来添加自定义字体的网页视图代码:

package com.example.sinhalafontrendering; 

import android.app.Activity; 
import android.graphics.Typeface; 
import android.os.Bundle; 
import android.view.Menu; 
import android.webkit.WebView; 
import android.widget.TextView; 

import com.example.sinhalafontrendering.R.id; 

public class MainActivity extends Activity { 
    WebView mWebView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     FontUtils.setCustomFont(findViewById(id.txtHelloWorld), getAssets()); 
     Typeface tf = Typeface.createFromAsset(getAssets(), "iskpota.ttf"); 
     TextView tv = (TextView) findViewById(id.txtHelloWorld); 
     tv.setText("ශ්‍රී ලංකා, \n "); 
     //tv.setTypeface(tf,0); 

     String html = "<html><head><style type=\"text/css\">@font-face { font-family: MyCustomFont; src: url(\"Bamini.eot\") /* EOT file for IE */}@font-face { font-family: MyCustomFont; src: url(\"file:///android_asset/iskpota.ttf\") /* TTF file for CSS3 browsers */}body { font-family: MyCustomFont, Verdana, Arial, sans-serif;font-size: medium;color: black}</style></head><body><LI><a href=\"http://www.thelastrow.lk/\">ශ්‍රී ලංකා;</a></LI></UL></div></body></html>"; 

     mWebView = (WebView) findViewById(R.id.webGoogle); 
     mWebView.getSettings().setJavaScriptEnabled(true); 
     mWebView.getSettings().setBuiltInZoomControls(true); 
     mWebView.getSettings().setSupportZoom(true); 
     //mWebView.loadUrl("file:///android_asset/a.html"); 
     mWebView.loadDataWithBaseURL("www.thelastrow.lk", html, "text/html", "UTF-8", null); 
     mWebView.setWebViewClient(new HelloWebViewClient()); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
} 
+0

你无法添加CSS? – KOTIOS

+0

我没有任何访问我想要在'WebView'内加载的网站。它应该像一个简单的浏览器窗口。但我不知道我是否可以为我在“WebView”中加载的网站添加“CSS”。 – Sajirupee

+0

我的意思是说你有谁使用加载方法将数据加载到webview?如果是的话,你可以添加CSS内部webview以及自定义字体 – KOTIOS

回答

0

它曾与loadDataWithBaseURL!

在这个例子中,我改变了CSS来:

@font-face { 
    font-family: 'feast'; 
    src: url('fonts/feasfbrg.ttf'); 
} 

body {font-family: 'feast';} 

然后用资产路径为基准网址:

loadDataWithBaseURL("www.google.com",myhtml,"text/html","utf-8",null); 
+0

试试这个代码 – KOTIOS

+0

所以我在哪里可以添加。像_www.google.com_这样的自定义网址,用我添加的字体加载该网站。 – Sajirupee

+0

它仍然没有加载我设置的网址de网络视图。你可以请张贴完整的例子... – Sajirupee