How do I open a Web site in an Android app? Google has provided us with a solution, so let's take a look at the WebView control now.
In order to facilitate the summary, to achieve the following effect as the main line, to summarize:
First we look at its layout file, the entire interface is divided into two parts, the upper is a similar to the title bar effect, it is composed of two button buttons and a TextView, the lower part is a WebView control, Remove the title of the system by Androidmanifest.xml (if you do not understand, please check my previous blog: Android Common Properties), has achieved the effect. For the convenience of self-study, the following code:
<linearlayout 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=". Mainactivity "> <LinearLayout android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:weightsum= "1" > <Button Android:id= "@+id/quit"android:layout_gravity= "Left"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Back"/> <TextView Android:id= "@+id/web"android:layout_gravity= "Center"android:gravity= "Center"Android:layout_width= "222DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1.13"/> <Button Android:id= "@+id/news"android:layout_gravity= "Right"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Refresh"/> </LinearLayout> <WebView Android:id= "@+id/webview"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"/></linearlayout>
Finally we begin to write our Mainactivity.java:
Public classMainactivityextendsActivity {PrivateTextView Mtextview; PrivateWebView Mwebview; PrivateButton Mbreak; PrivateButton mnews; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Init (); } Public voidinit () {Mtextview=(TextView) Findviewbyid (R.id.web); Mwebview=(WebView) Findviewbyid (R.id.webview); Mbreak=(Button) Findviewbyid (r.id.quit); Mnews=(Button) Findviewbyid (r.id.news); Mbreak.setonclicklistener (NewMyListener ()); Mnews.setonclicklistener (NewMyListener ()); Mwebview.loadurl ("http://www.baidu.com/");//set the Open URLMwebview.setwebchromeclient (Newwebchromeclient () {@Override Public voidOnreceivedtitle (WebView view, String title) {Super. Onreceivedtitle (view, title); Mtextview.settext (title);//Show open URL information } }); Mwebview.setwebviewclient (Newwebviewclient () {@Override Public Booleanshouldoverrideurlloading (WebView view, String URL) {view.loadurl (URL); return Super. shouldoverrideurlloading (view, URL); } }); } //button click event Listener classMyListenerImplementsview.onclicklistener{@Override Public voidOnClick (view view) {Switch(View.getid ()) { CaseR.id.quit:finish (); Break; Caser.id.news:mwebview.reload (); Break; } } }
Finally, do not forget to add the Use network declaration in Androidmanifest.xml: <uses-permission android:name= "Android.permission.INTERNET"/>
This concludes our initial introduction to the WebView.
<uses-permission android:name= "Android.permission.INTERNET"/>
WebView of Android controls