我不知道如何更改Webview的字體。Webview更改字體? _android
現在下載HTML和CSS樣式應用到其他方式改變字體嗎?
另外,我希望網站實時更改字體。
我該怎麼做。
-------------我的源------
public class WebviewActivity extends Activity {
/** Called when the activity is first created. */
TextView tx;
String html;
WebView webview;
WebSettings webset;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview=(WebView)findViewById(R.id.webView1);
html="http://naver.com";
webview.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
html=DownloadHtml(url);
webview.loadDataWithBaseURL(null, getHtmlData(WebviewActivity.this,html) , "text/html", "UTF-8", "about:blank");
return super.shouldOverrideUrlLoading(view, url);
}
});
copyFile(this.getBaseContext(), "aa.TTF");
webset=webview.getSettings();
webset.setJavaScriptEnabled(true);
webview.loadUrl(html);
}
private boolean copyFile(Context context,String fileName) {
boolean status = false;
try {
FileOutputStream out = context.openFileOutput(fileName, Context.MODE_PRIVATE);
InputStream in = context.getAssets().open(fileName);
// Transfer bytes from the input file to the output file
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Close the streams
out.close();
in.close();
status = true;
} catch (Exception e) {
System.out.println("Exception in copyFile:: "+e.getMessage());
status = false;
}
System.out.println("copyFile Status:: "+status);
return status;
}
private String getHtmlData(Context context, String data){
String head = "<head><style>@font-face {font-family: 'aa';src: url('file://"+ context.getFilesDir().getAbsolutePath()+ "/aa.TTF');}body {font-family: 'aa';}</style></head>";
String htmlData= "<html>"+head+"<body>"+data+"</body></html>" ;
return htmlData;
}
String DownloadHtml(String addr) {
HttpGet httpget = new HttpGet(addr);
DefaultHttpClient client = new DefaultHttpClient();
StringBuilder html = new StringBuilder();
try {
HttpResponse response = client.execute(httpget);
BufferedReader br = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
for (;;) {
String line = br.readLine();
if (line == null)
break;
html.append(line + '\n');
}
br.close();
} catch (Exception e) {
;
}
return html.toString();
}
}
感謝you.However,我的客戶正在使用Android想改變font.Rather比使用Android網頁看看有沒有辦法改變嗎? 請理解我的英語很差。祝你今天愉快!! – user1101062 2011-12-16 06:03:21