WebView字号设置

下面示例是通过改变WebSettings的TextSize值来改变字号的,如果是html文本也可以用js来改变富文本的字体大小。

测试activity:

package com.home.webviewsize;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;

public class TestWebViewSizeActivity extends Activity implements
		OnClickListener {
	private WebView webView;
	private Button enlargeBtn;
	private Button shrinkBtn;
	private WebSettings settings;
	private int fontSize = 1;// 用来控制字体大小

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		enlargeBtn = (Button) findViewById(R.id.main_btn_enlarge);
		shrinkBtn = (Button) findViewById(R.id.main_btn_shrink);
		enlargeBtn.setOnClickListener(this);
		shrinkBtn.setOnClickListener(this);
		webView = (WebView) findViewById(R.id.main_webview);
		settings = webView.getSettings();
		settings.setSupportZoom(true);
		getNetData();// 加载网络数据
		getInitialFontSize();// 得到文章字体的大小

	}

	/**
	 * 得到文章字体的大小
	 */
	private void getInitialFontSize() {
		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 getNetData() {
		new Thread() {
			public void run() {
				webView.loadUrl("http://blog.csdn.net/u010142437/article/details/12679267");
			}
		}.start();
	}

	@Override
	public void onClick(View v) {
		if (v == shrinkBtn) {
			fontSize--;
			refreshUI();// 根据字体大小刷新UI
		}
		if (v == enlargeBtn) {
			fontSize++;
			refreshUI();// 根据字体大小刷新UI
		}
	}

	/**
	 * 根据字体大小刷新UI
	 */
	private void refreshUI() {
		if (fontSize > 5) {
			fontSize = 5;
		}
		if (fontSize < 1) {
			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;
		}
	}
}
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/main_layout_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/main_btn_enlarge"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="放大" />

        <Button
            android:id="@+id/main_btn_shrink"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="缩小" />
    </LinearLayout>

    <WebView
        android:id="@+id/main_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/main_layout_bottom" />

</RelativeLayout>
记得加上访问网络的权限:
<uses-permission android:name="android.permission.INTERNET" />




你可能感兴趣的:(android,webView,WebSettings,加载网络资源,改变字号)