javascript 调整Android WebView的字体大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4188168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adjust font size of Android WebView
提问by hpique
How do you adjust the font size of an Android WebView? The following appears to have no effect:
你如何调整Android的字体大小WebView?以下似乎没有效果:
private void fontSizePlus() {
fontSize = (fontSize < FONT_SIZE_MAX) ? fontSize + FONT_SIZE_INCREMENT : fontSize;
this.changeFontSize(fontSize);
}
private void fontSizeMinus() {
fontSize = (fontSize > FONT_SIZE_MIN) ? fontSize - FONT_SIZE_INCREMENT : fontSize;
this.changeFontSize(fontSize);
}
private void changeFontSize(int value) {
String js = "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '" + value + "%';";
mWebView.loadUrl("javascript:(function() { " + js + " })()");
}
Where the WebViewand constants have been initialized as follows:
其中WebView和 常量已初始化如下:
private final static int FONT_SIZE_DEFAULT = 100;
private final static int FONT_SIZE_MIN = 50;
private final static int FONT_SIZE_MAX = 150;
private final static int FONT_SIZE_INCREMENT = 5;
private int fontSize = FONT_SIZE_DEFAULT;
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.index);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/index.htm");
}
回答by hpique
@rob's answer and comment pointed me in the right direction.
@rob 的回答和评论为我指明了正确的方向。
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).
通过更改默认字体大小的值,所有相关字体大小都会相应调整。这为您提供了比 更大的控制权WebSettings.setTextSize (WebSettings.TextSize t)。
回答by rob
I don't know that webkitTextSizeAdjustis supported in anything other than safari/mobile safari.
我不知道webkitTextSizeAdjustsafari/mobile safari 以外的任何东西都支持它。
Try WebSettings.setTextSize (WebSettings.TextSize t)
尝试 WebSettings.setTextSize (WebSettings.TextSize t)
rather than trying to do it with javascript.
而不是尝试用javascript来做。
回答by Basim Sherif
Brian Cooley helped me with this...try it...
Brian Cooley 帮我解决了这个问题……试试看……
increaseFontButton.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
WebSettings settings = myWebView.getSettings();
settings.setTextZoom( (int)(settings.getTextZoom() * 1.1) );
}
}

