How to change the FontSize in an Android WebView?

夙愿已清 提交于 2019-11-26 05:28:17

问题


How can you manually change the font size of a webview? e.g. When the page loads up in the webview the font size is like 24pt. and way too large for my android\'s screen. I\'ve looked into the \"websettings\" but it seems that the two are not related.

Thanks


回答1:


I finally found it:-

WebSettings webSettings = webView.getSettings();

either setTextSize or

webSettings.setTextSize(WebSettings.TextSize.SMALLEST);

This one works too:-

webSettings.setDefaultFontSize(10);



回答2:


It seems that nowadays prefered way, ie not depreciated is to change text zoom, like this:

WebSettings settings = mWebView.getSettings();
settings.setTextZoom(90); // where 90 is 90%; default value is ... 100



回答3:


This is what I use when I want to enable the user to change the text size / zoom in a 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);
}

On Actionbar Item click, I call either textSmaller() or textBigger() to change the text size.




回答4:


I use Javascript to do these kind of things because it practically always works. Even if there are CSS files used in your HTML

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

ofcourse you need to alter the sizes and colors to the ones you need




回答5:


If you want to increase or decrease font Size of WebView dynamycally than use these lines of code:

WebView mWebView;
int fontSize;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mWebView = findViewById(R.id.webview);
    mWebView.loadUrl("file:///android_asset/sample.html");
    // enable / disable javascript
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setSupportZoom(true);
    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.getSettings().setDisplayZoomControls(true);
    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);
}

public void Inc(View view) {
    fontSizePlus();
}

public void Dec(View view) {
    fontSizeMinus();
}


来源:https://stackoverflow.com/questions/3796176/how-to-change-the-fontsize-in-an-android-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!