【问题标题】:Webview change font ? _androidWebview 更改字体? _安卓
【发布时间】:2012-01-21 17:23:07
【问题描述】:

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

现在下载用于更改字体的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();
    }
}

【问题讨论】:

    标签: android fonts webview


    【解决方案1】:

    不改变网页内容就不能改变字体。

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

    请关注What is WebVIew

    【讨论】:

    • 谢谢。不过我的客户端是用Android的,想改字体。而不是用Android的网页,有办法改吗?请理解我的英语很差。祝你有美好的一天!
    【解决方案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;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 2011-05-28
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-16
      相关资源
      最近更新 更多