改变WebView字体大小效果:
直接上代码:
package com.example.zhihuibj; import java.lang.annotation.Annotation; import com.lidroid.xutils.view.annotation.event.OnChildClick; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.graphics.Bitmap; import android.os.Bundle; import android.text.AlteredCharSequence; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebSettings.TextSize; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ImageButton; import android.widget.ProgressBar; public class WebViewActivity extends Activity implements OnClickListener{ private ProgressBar mProgressBar; private WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.webview_newsdatail); mWebView = (WebView) findViewById(R.id.wb_webview_newsdatail_webviewcontent); ImageButton mImageButton_Back = (ImageButton) findViewById(R.id.ib_webview_newsdatail_back); ImageButton mImageButton_textsize= (ImageButton) findViewById(R.id.ib_webview_newsdatail_textsize); ImageButton mImageButton_share =(ImageButton) findViewById(R.id.ib_webview_newsdatail_share); mProgressBar = (ProgressBar) findViewById(R.id.pb_webview_newsdatail_progressbar); mImageButton_Back.setOnClickListener(this); mImageButton_textsize.setOnClickListener(this); mImageButton_share.setOnClickListener(this); //根据键值获得url String webview_url=getIntent().getStringExtra("webview_url");//得到URL WebSettings webview_settings = mWebView.getSettings(); //使能JavaScript webview_settings.setJavaScriptEnabled(true); //显示放大缩小按钮 webview_settings.setDisplayZoomControls(true); //双击放大或缩放 webview_settings.setUseWideViewPort(true); /* * 当前作用:用于获得页面加载的状态:开始加载,加载结束,在本应用中加载 */ mWebView.setWebViewClient(new WebViewClient(){ /* * 所有跳转的链接都会在此回调中进行调用 * */ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { System.out.println("跳转URL ..."+url); view.loadUrl(url);//防止调用系统自带的浏览器打开网页 return true; } /* * 开始加载网页 * @see android.webkit.WebViewClient#onPageStarted(android.webkit.WebView, java.lang.String, android.graphics.Bitmap) */ @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { // TODO Auto-generated method stub super.onPageStarted(view, url, favicon); System.out.println("onPageStarted.... "); mProgressBar.setVisibility(View.VISIBLE); } /* * 加载网页结束 * @see android.webkit.WebViewClient#onPageFinished(android.webkit.WebView, java.lang.String) */ @Override public void onPageFinished(WebView view, String url) { // TODO Auto-generated method stub super.onPageFinished(view, url); System.out.println("onPageFinished.... "); mProgressBar.setVisibility(View.INVISIBLE); } }); /* * 当前作用:获得网页信息:得到网页标题,网页图标,网页加载进度 */ mWebView.setWebChromeClient(new WebChromeClient(){ /* * 获得webView的标题 * @see android.webkit.WebChromeClient#onReceivedTitle(android.webkit.WebView, java.lang.String) */ @Override public void onReceivedTitle(WebView view, String title) { // TODO Auto-generated method stub super.onReceivedTitle(view, title); System.out.println("title : "+title); } /* * 获得标题图标 * @see android.webkit.WebChromeClient#onReceivedIcon(android.webkit.WebView, android.graphics.Bitmap) */ @Override public void onReceivedIcon(WebView view, Bitmap icon) { // TODO Auto-generated method stub super.onReceivedIcon(view, icon); } /* * 加载进度 * @see android.webkit.WebChromeClient#onProgressChanged(android.webkit.WebView, int) */ @Override public void onProgressChanged(WebView view, int newProgress) { // TODO Auto-generated method stub super.onProgressChanged(view, newProgress); System.out.println("newProgress :"+newProgress); } }); mWebView.loadUrl(webview_url); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.ib_webview_newsdatail_back: finish(); break; case R.id.ib_webview_newsdatail_share: break; case R.id.ib_webview_newsdatail_textsize: ChangeWebViewTextSize(); break; default: break; } } /************************* 改变网页字体功能 开始************************************/ private int mCurrentDialog=0; private void ChangeWebViewTextSize() { AlertDialog.Builder builder = new AlertDialog.Builder(this); String[] items = new String[]{"超大号字体","大号字体","普通字体","小号字体","超小号字体"}; builder.setTitle("设置字体"); builder.setSingleChoiceItems(items,2, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mCurrentDialog=which; } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { WebSettings webview_Dialog_settings = mWebView.getSettings(); switch (mCurrentDialog) { case 0: webview_Dialog_settings.setTextSize(TextSize.LARGEST); break; case 1: webview_Dialog_settings.setTextSize(TextSize.LARGER); break; case 2: webview_Dialog_settings.setTextSize(TextSize.NORMAL); break; case 3: webview_Dialog_settings.setTextSize(TextSize.SMALLER); break; case 4: webview_Dialog_settings.setTextSize(TextSize.SMALLEST); break; default: break; } } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } /************************* 改变网页字体功能 结束************************************/ }