Android中的WebView进行直接加载网页

package com.webview;

import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebSettings.TextSize;

public class MainActivity extends Activity {

	private WebView web_about_us;
	private String url = "http://www.baidu.com/";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		web_about_us = (WebView) findViewById(R.id.web_about_us);
		/**
		 * web控件相关
		 */
		web_about_us.getSettings().setLoadWithOverviewMode(true);// 自适应屏幕.
		web_about_us.getSettings().setSupportZoom(true);
		web_about_us.getSettings().setTextSize(TextSize.LARGEST);
		web_about_us.setInitialScale(80);// 设置初始缩放大小
		web_about_us.getSettings().setDefaultTextEncodingName("utf-8"); // 页面编码方式.
		web_about_us.loadUrl(url);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (web_about_us.canGoBack() && event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			web_about_us.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/white"
        android:orientation="vertical" >

        <WebView
            android:id="@+id/web_about_us"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

</RelativeLayout>


你可能感兴趣的:(Android中的WebView进行直接加载网页)