我想开发一个简单的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;
}
}
你无法添加CSS? – KOTIOS
我没有任何访问我想要在'WebView'内加载的网站。它应该像一个简单的浏览器窗口。但我不知道我是否可以为我在“WebView”中加载的网站添加“CSS”。 – Sajirupee
我的意思是说你有谁使用加载方法将数据加载到webview?如果是的话,你可以添加CSS内部webview以及自定义字体 – KOTIOS