【转】android webview设置内容的字体大小

时间:2023-03-08 19:36:40

Enum for specifying the text size. SMALLEST is 50% SMALLER is 75% NORMAL is 100% LARGER is 150% LARGEST is 200%
原文:http://www.cnblogs.com/yourancao520/archive/2012/06/14/2548900.html
 /**
  * webview
  */
 WebView wv;
 /**
  * Manages settings state for a WebView
  */
 WebSettings settings;
 /**
  * 用来控制字体大小
  */
 int fontSize = 1;

wv = (WebView) findViewById(R.id.webViewContent);
  settings = wv.getSettings();
  settings.setSupportZoom(true);

if (settings.getTextSize() == WebSettings.TextSize.SMALLEST) {
   fontSize = 1;
  } else if (settings.getTextSize() == WebSettings.TextSize.SMALLER) {
   fontSize = 2;
  } else if (settings.getTextSize() == WebSettings.TextSize.NORMAL) {
   fontSize = 3;
  } else if (settings.getTextSize() == WebSettings.TextSize.LARGER) {
   fontSize = 4;
  } else if (settings.getTextSize() == WebSettings.TextSize.LARGEST) {
   fontSize = 5;
  }

/**
     * 设置底部按钮的事件
     */
    private void setImageViewClick() {

/**
         * 缩小按钮
         */
        imgViewSX.setOnClickListener(new OnClickListener() {

@Override
            public void onClick(View v) {

fontSize--;

if (fontSize < 0) {
                    fontSize = 1;
                }
                switch (fontSize) {

case 1:
                    settings.setTextSize(WebSettings.TextSize.SMALLEST);
                    break;
                case 2:
                    settings.setTextSize(WebSettings.TextSize.SMALLER);
                    break;
                case 3:
                    settings.setTextSize(WebSettings.TextSize.NORMAL);
                    break;
                case 4:
                    settings.setTextSize(WebSettings.TextSize.LARGER);
                    break;
                case 5:
                    settings.setTextSize(WebSettings.TextSize.LARGEST);
                    break;
                }

}
        });

/**
         * 放大按钮
         */
        imgViewFD.setOnClickListener(new OnClickListener() {

@Override
            public void onClick(View v) {

fontSize++;

if (fontSize > 5) {
                    fontSize = 5;
                }
                switch (fontSize) {

case 1:
                    settings.setTextSize(WebSettings.TextSize.SMALLEST);
                    break;
                case 2:
                    settings.setTextSize(WebSettings.TextSize.SMALLER);
                    break;
                case 3:
                    settings.setTextSize(WebSettings.TextSize.NORMAL);
                    break;
                case 4:
                    settings.setTextSize(WebSettings.TextSize.LARGER);
                    break;
                case 5:
                    settings.setTextSize(WebSettings.TextSize.LARGEST);
                    break;
                }
            }
        });

final String mimeType = "text/html";
        final String encoding = "UTF-8";

content = getContentFromNewWork(newsId);
wv.loadDataWithBaseURL("", content, mimeType, encoding, "");