Webview change font ? _android

2019-06-03 17:33发布

问题:

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

回答1:

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



回答2:

// Put your font file in assets

private WebView wv;

wv = (WebView)findViewById(R.id.wv);
wv.setBackgroundColor(Color.BLACK);

String my_text=getData("I am android developer");

wv.loadDataWithBaseURL("file:///android_asset//Gill Sans MT.ttf", t, "text/html", "UTF-8", "null" );

private String getData(String s){

    String htmlData="<font color='white'>" + s + "</font>";
     String PAGE_HTML =
            "<html>\n" +
            "  <style type=\"text/css\"> \n" + 
            "   @font-face { \n" + 
            "       font-family: MyFont; \n" + 
            "       src: url(\"file:///android_asset/Gill Sans MT.ttf\") \n" + 
            "   } \n" + 
            "   body { \n" + 
            "       font-family: MyFont; \n" + 
            "       font-size: medium; \n" + 
            "       text-align: justify; \n" + 
            "   } \n" + 
            "  </style> \n" + 
            "  <body>\n" + 
            "    "+htmlData+"<br>\n" + 

            "  </body>\n" + 
            "</html>";

    return PAGE_HTML;

}