Android-控件WebView的基本使用

在安卓开发过程中,很多时候我们需要用到WebView控件,下面以一个例子进行说明。

功能描述:进入界面后自动加载绑定的网址。

1、首先需要完成界面的开发,代码如下所示(activity_history):

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    tools:context=".HistoryData" >

<WebView
    android:id="@+id/webView2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />
	
</RelativeLayout>

2、在第一步,我们已经完成了界面的开发,接下来打开对应的java文件,编写如下代码:

package com.sw.xmk;

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

public class HistoryData extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_history);
		
		WebView webview = (WebView)findViewById(R.id.webView2);
		//支持javascript
		webview.getSettings().setJavaScriptEnabled(true);
		//加载网页
		webview.loadUrl("http://10.0.2.2:8080/xmk/webpage/menu/allData.action");
		
//		WebSettings websettings = webview.getSettings();
//		//居中
//		//websettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
//		//自适应
//		websettings.setUseWideViewPort(true);
//		websettings.setLoadWithOverviewMode(true);
		//手指缩放
		webview.getSettings().setSupportZoom(true);
		webview.getSettings().setBuiltInZoomControls(true);
	}
}

注意:别忘了还还要在AndroidManifest.xml中进行activity的声明。

运行效果如下所示:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章