1、首先创建一个空白Android项目
2、然后打开项目,切换为Android视图,这时候会看到三个文件夹,分别是manifests、java、res。首先修改res/layout下的activity_web.xml布局文件,内容为:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
3、修改java文件夹项目下的MainActibity.java文件。该文件就是程序的入口,也是默认的程序首页,内容为一个TextView控件。
package com.irunker.visant;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
@SuppressLint("SetJavaScriptEnabled")
public class main extends AppCompatActivity {
private WebView myWebView = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
// 打开网页
myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.irunker.com/");
//设置可自由缩放网页、JS生效
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
// 修改默认在WebView中打开链接
myWebView.setWebViewClient(new WebViewClient());
}
// 按键响应,在WebView中查看网页时,按返回键的时候按浏览历史退回,如果不做此项处理则整个WebView返回退出
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
{
// 返回键退回
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
4、修改程序的主配置文件AndroidManifest.xml。 该文件声明程序中的Activities, ContentProviders, Services, 和Intent Receivers,permissions和instrumentation等。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.irunker.visant">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
5、最后修改一下res/value文件夹下的styles.xml样式.保证无多余的顶部样式,只是显示一个网页。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>