BKJIA once exclusively recommended a special topic for Android development and application explanation. This article hopes to show you how to use WebView components in detail:
Network Content
1. LoadUrl directly displays the webpage content (displays the network image separately)
2. LoadData displays Chinese webpage content (including space processing)
Files in the APK package
1. LoadUrl: display Html and image files in APK
2. LoadData (loadDataWithBaseURL) displays Html content mixed with images and text in the APK.
Res/layout/main. xml
Xml Code
- < ?xml version="1.0" encoding="utf-8"?>
-
- < LINEARLAYOUT android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
-
- < WEBVIEW android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/webview" />
-
- < /LINEARLAYOUT>
-
- < ?xml version="1.0" encoding="utf-8"?>
-
- < LINEARLAYOUT android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
-
- < WEBVIEW android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/webview" />
-
- < /LINEARLAYOUT>
-
- Example_webview.java
Java code
- Package cn. coolworks;
-
- Import java.net. URLEncoder;
-
- Import android. app. Activity;
-
- Import android. OS. Bundle;
-
- Import android. webkit. WebView;
-
- Public class Example_webview extends Activity {
-
- WebView webView;
-
- Final String mimeType = "text/html ";
-
- Final String encoding = "UTF-8 ";
-
- /** Called when the activity is first created .*/
-
- @ Override
-
- Public void onCreate (Bundle savedInstanceState ){
-
- Super. onCreate (savedInstanceState );
-
- SetContentView (R. layout. main );
-
- WebView = (WebView) findViewById (R. id. webview );
-
- WebView. getSettings (). setJavaScriptEnabled (true );
-
- //
-
- // WebHtml ();
-
- //
-
- // WebImage ();
-
- //
-
- // LocalHtmlZh ();
-
- //
-
- // LocalHtmlBlankSpace ();
-
- //
-
- // LocalHtml ();
-
- //
-
- // LocalImage ();
-
- //
-
- LocalHtmlImage ();
-
- }
-
- /**
-
- * Direct webpage display
-
- */
-
- Private void webHtml (){
-
- Try {
-
- WebView. loadUrl ("http://www.google.com ");
-
- } Catch (Exception ex ){
-
- Ex. printStackTrace ();
-
- }
-
- }
-
- /**
-
- * Direct network image display
-
- */
-
- Private void webImage (){
-
- Try {
-
- WebView
-
- . LoadUrl ("http://www.gstatic.com/codesite/ph/images/code_small.png ");
-
- } Catch (Exception ex ){
-
- Ex. printStackTrace ();
-
- }
-
- }
-
- /**
-
- * Chinese display
-
- */
-
- Private void localHtmlZh (){
-
- Try {
-
- String data = "test Html data containing Chinese characters ";
-
- // UTF-8 encoding (garbled characters may occur on SDK1.5 simulators and real devices, and can be normally displayed on SDK1.6)
-
- // WebView. loadData (data, mimeType, encoding );
-
- // Encode the data (SDK1.5)
-
- WebView. loadData (URLEncoder. encode (data, encoding), mimeType,
-
- Encoding );
-
- } Catch (Exception ex ){
-
- Ex. printStackTrace ();
-
- }
-
- }
-
- /**
-
- * Chinese display (processing of spaces)
-
- */
-
- Private void localHtmlBlankSpace (){
-
- Try {
-
- String data = "testing Html data with spaces ";
-
- // Do not process Spaces
-
- WebView. loadData (URLEncoder. encode (data, encoding), mimeType,
-
- Encoding );
-
- // WebView. loadData (data, mimeType, encoding );
-
- // Process spaces (in SDK1.5)
-
- WebView. loadData (URLEncoder. encode (data, encoding). replaceAll (
-
- "\ +", ""), MimeType, encoding );
-
- } Catch (Exception ex ){
-
- Ex. printStackTrace ();
-
- }
-
- }
-
- /**
-
- * Display local image files
-
- */
-
- Private void localImage (){
-
- Try {
-
- // Local file processing (if the file name contains spaces, use + to replace it)
-
- WebView. loadUrl ("file: // android_asset/icon.png ");
-
- } Catch (Exception ex ){
-
- Ex. printStackTrace ();
-
- }
-
- }
-
- /**
-
- * Display local webpage files
-
- */
-
- Private void localHtml (){
-
- Try {
-
- // Local file processing (if the file name contains spaces, use + to replace it)
-
- WebView. loadUrl ("file: // android_asset/test.html ");
-
- } Catch (Exception ex ){
-
- Ex. printStackTrace ();
-
- }
-
- }
-
- /**
-
- * Display Html content mixed with local images and text
-
- */
-
- Private void localHtmlImage (){
-
- Try {
-
- String data = "test the mixed display of local images and text. This is an image in APK ";
-
- // SDK1.5 local file processing (Images cannot be displayed)
-
- // WebView. loadData (URLEncoder. encode (data, encoding), mimeType,
-
- // Encoding );
-
- // SDK1.6 and later versions
-
- // WebView. loadData (data, mimeType, encoding );
-
- // Process local files (images can be displayed)
-
- WebView. loadDataWithBaseURL ("about: blank", data, mimeType,
-
- Encoding ,"");
-
- } Catch (Exception ex ){
-
- Ex. printStackTrace ();
-
- }
-
- }
-
- }