如何在Android WebView中更改FontSize?

Sco*_*ott 38 android font-size android-webview android-fonts

如何手动更改webview的字体大小?例如,当在webview中加载页面时,字体大小就像是24pt.对于我的android屏幕来说太大了.我查看了"网络设置",但似乎两者没有关系.

谢谢

Sco*_*ott 101

我终于找到了: -

WebSettings webSettings = webView.getSettings();
Run Code Online (Sandbox Code Playgroud)

无论是setTextSize

webSettings.setTextSize(WebSettings.TextSize.SMALLEST);
Run Code Online (Sandbox Code Playgroud)

这个也适用: -

webSettings.setDefaultFontSize(10);
Run Code Online (Sandbox Code Playgroud)

  • 不推荐使用webSettings.setTextSize(WebSettings.TextSize.SMALLEST) (9认同)

pel*_*lus 31

似乎现在更喜欢的方式,即不折旧是改变文字缩放,如下所示:

WebSettings settings = mWebView.getSettings();
settings.setTextZoom(90); // where 90 is 90%; default value is ... 100
Run Code Online (Sandbox Code Playgroud)


Phi*_*oda 17

当我想让用户更改文本大小/缩放时,我就会使用这个WebView:

private WebView mWebView;

// init web view and stuff like that ...


private void textSmaller() {

    WebSettings settings = mWebView.getSettings();
    settings.setTextZoom(settings.getTextZoom() - 10);
}

private void textBigger() {

    WebSettings settings = mWebView.getSettings();
    settings.setTextZoom(settings.getTextZoom() + 10);
}
Run Code Online (Sandbox Code Playgroud)

Actionbar项目单击时,我调用textSmaller()textBigger()更改文本大小.


bva*_*sen 7

我使用Javascript来做这些事情,因为它几乎总是有效.即使您的HTML中使用了CSS文件

mWebView.loadUrl("javascript:(document.body.style.backgroundColor ='red');");
mWebView.loadUrl("javascript:(document.body.style.color ='yellow');");
mWebView.loadUrl("javascript:(document.body.style.fontSize ='20pt');");
Run Code Online (Sandbox Code Playgroud)

当然,你需要改变你需要的尺寸和颜色