android - 网页 View 更改字体? _安卓

标签 android fonts webview

我不知道如何更改 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();
    }
}

最佳答案

如果不更改网页内容,就无法更改字体。

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

请看What is WebVIew

关于android - 网页 View 更改字体? _安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8529839/

相关文章:

java - 使用 Javascript 从 WebView 调用 Activity 的方法

java - Android - 以编程方式设置警报对话框上的文本颜色列表项

Java HashMap 在循环迭代中丢失值

android - 应用程序在后台时的 BroadcastReceiver

html - 字体似乎不起作用

c# - 使用 Font 创建条形码 - 这是否正确?我错过了什么吗?

android - 如何删除在 firebase 控制台中更新数据后获得的重复数据

ios - iOS 应用程序中的 Helvetica Neue 字体

javascript - 如何检测 WebView Android 中的按钮点击?

c# - WebView 调用脚本 HRESULT 0x80020101