Adjust font size of Android WebView

二次信任 提交于 2019-12-03 04:44:30
hpique

@rob's answer and comment pointed me in the right direction.

First make sure that all font sizes are relative to the default font size. If you use absolute values the following will not work on those elements.

Then:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    mWebView = (WebView) findViewById(R.id.webview);
    fontSize = mWebView.getSettings().getDefaultFontSize();
    ...
}

private void fontSizePlus() {
    fontSize++;
    this.changeFontSize(fontSize);
}

private void fontSizeMinus() {
    fontSize--;
    this.changeFontSize(fontSize);
}

private void changeFontSize(int value) {
    mWebView.getSettings().setDefaultFontSize(value);
}

By changing the value of the default font size all relative font sizes are adjusted accordingly. This gives you much greater control than WebSettings.setTextSize (WebSettings.TextSize t).

rob

I don't know that webkitTextSizeAdjust is supported in anything other than safari/mobile safari.

Try WebSettings.setTextSize (WebSettings.TextSize t)

rather than trying to do it with javascript.

Basim Sherif

Brian Cooley helped me with this...try it...

increaseFontButton.setOnClickListener( new OnClickListener() {
    public void onClick(View v) {
        WebSettings settings = myWebView.getSettings();
        settings.setTextZoom( (int)(settings.getTextZoom() * 1.1) );
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!