Activity_main.xml
<Relativelayoutxmlns: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"Tools:context=". Mainactivity " > <WebViewAndroid:id= "@+id/webview1"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_alignparentbottom= "true"Android:layout_alignparenttop= "true" /></Relativelayout>
Mainactivity.java
PackageCom.example.webviewtest1;ImportAndroid.os.Bundle;Importandroid.app.Activity;Importandroid.view.KeyEvent;ImportAndroid.view.Menu;ImportAndroid.webkit.WebView;Importandroid.webkit.WebViewClient; Public classMainactivityextendsActivity {PrivateWebView WebView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //Add by Liya at 2015-9-10WebView =(WebView) Findviewbyid (R.ID.WEBVIEW1); Webview.getsettings (). setjavascriptenabled (true); Webview.setwebviewclient (Newwebviewclient () {@Override Public Booleanshouldoverrideurlloading (WebView view, String URL) {view.loadurl (URL); return true; } }); Webview.loadurl ("Http://www.baidu.com"); } @Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { if((keycode = = Keyevent.keycode_back) &&Webview.cangoback ()) {Webview.goback (); return true; } return false; } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } }
Manifest
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.webviewtest1"Android:versioncode= "1"Android:versionname= "1.0" > <uses-permission android:name= "Android.permission.INTERNET" /> <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" /> <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <ActivityAndroid:name= "Com.example.webviewtest1.MainActivity"Android:label= "@string/app_name" > <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> </Application></Manifest>
Android WebView Demo