Webview更改字体? _android [英] Webview change font ? _android

查看:159
本文介绍了Webview更改字体? _android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何改变Webview的字体。

现在下载html和css样式应用到其他方式改变字体吗?

/ p>

另外,我希望网站能够实时更改字体。

我应该怎么做。



------------- my source ------

  public class WebviewActivity extends Activity {
/ **在第一次创建活动时调用。 * /
TextView tx;
String html;
WebView webview;
Web设置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自动生成方法存根
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;
尝试{
FileOutputStream out = context.openFileOutput(fileName,Context.MODE_PRIVATE);
InputStream in = context.getAssets()。open(fileName);
//从输入文件传输字节到输出文件
byte [] buf = new byte [1024];
int len; ((len = in.read(buf))> 0){
out.write(buf,0,len);

//关闭流
out.close();
in.close();
status = true;
} catch(Exception e){
System.out.println(CopyFile ::+ e.getMessage())中的异常)
status = false;
}
System.out.println(copyFile Status ::+ 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> ;
返回htmlData;


String DownloadHtml(String addr){
HttpGet httpget = new HttpGet(addr);
DefaultHttpClient client = new DefaultHttpClient();
StringBuilder html = new StringBuilder();
尝试{
HttpResponse响应= client.execute(httpget);
BufferedReader br = new BufferedReader(new InputStreamReader(
response.getEntity()。getContent())); (b; b)
($; $ b $ String line = br.readLine();
if(line == null)
break;
html.append(line +'\\\
');
}
br.close();
} catch(Exception e){
;
}
return html.toString();


$ / code $ / pre

解决方案

如果不改变网页的内容,你不能改变字体。



WebView基本上是一个显示网页的视图 该网页可能是静态的(如html)或动态的。但是它不会改变网页的外观和感觉。所以它会显示与网页完全相同的文字。如果您需要更改字体,则必须在网页中更改它(可能是html)。



请查看什么是WebVIew


I don't know that how to change the font for Webview.

Now download the html and css styles applied to change the font there any other way?

also, I want website the font change in real time.

What should I do.

-------------my source------

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();
    }
}

解决方案

Without making change in the content of webpage you cant change the font.

WebView is basically a view to display web pages That web pages may be static(eg html) or dynamic. But it doesn't change the look and feel of web page. so It wil display the text exactly same as that of webpage. If you need to change the font then you have to change it in webpage (may be in html).

Please have a look on What is WebVIew

这篇关于Webview更改字体? _android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆