At present, many android apps can display the interface of web pages, embedded development, this interface is generally loaded by the WebView control, learning this control can improve the scalability of your app development.
Let's talk about some of the advantages of WebView:
- Can directly display and render web pages, directly display web pages
- webview can directly use html files (on the network or in local assets) for layout
- Interact with JavaScript
- Click event of a page tag
Effect: (The top of the webpage is scrolling with JS effect, 4 modules can realize click events, and information prompts can be seen)
activity_main.xml
<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" tools:context="${relativePackage}.${activityClass}" > <WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/webview" /> </RelativeLayout>
MainActivity.java
public class MainActivity extends Activity { public String URL = "http://bajie.zhangwoo.cn/app.php?platform=android&appkey=5a379b5eed8aaae531df5f60b12100cfb6dff2c1&c=travel&a=home"; WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); webView = (WebView) findViewById(R.id.webview); webView.loadUrl(URL); initView(); } @SuppressLint("SetJavaScriptEnabled") private void initView() { // TODO Auto-generated method stub webView.requestFocus(); webView.setHorizontalScrollBarEnabled(true); webView.setVerticalScrollBarEnabled(true); WebSettings web = webView.getSettings(); web.setJavaScriptEnabled(true);// Enable support for javascript web.setBuiltInZoomControls(true); web.setSupportZoom(true); // Whether the screen double-click zoom is supported, but the following is the premise web.setDefaultTextEncodingName("utf-8");// Set the encoding format // Override WebView's default behavior of using a third-party or system default browser to open web pages, so that web pages are opened with WebView webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub // When the return value is true, the control goes to the WebView to open, and if it is false, the system browser or a third-party browser is called if (url.indexOf("zwapp://showlist/?tab=zhoubian") != -1) { Toast.makeText(getApplicationContext(), "Around Tour", 1).show(); } else if (url.indexOf("zwapp://showlist/?tab=gonglue") != -1) { Toast.makeText(getApplicationContext(), "Travel Tips", 1).show(); } else if (url.indexOf("zwapp://showlist/?tab=zhaiguo") != -1) { Toast.makeText(getApplicationContext(), "摘果", 1).show(); } else if (url.indexOf("zwapp://showlist/?tab=gongyuan") != -1) { Toast.makeText(getApplicationContext(), "Theme Park", 1).show(); } else { } return true; } }); } }
Remember to add network permissions
<uses-permission android:name="android.permission.INTERNET"/>