您的位置:首页 > 移动开发

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

2014-03-18 14:28 525 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: