Introduction to webview (Basic display)

Source: Internet
Author: User

Edited from: http://developer.51cto.com/art/201008/216488.htm
51cto once exclusively recommended the topic of Android development and application explanation. This article hopes to show you how to use webview components in detail:
I. Network Content
1. loadurl directly displays the webpage content (displays the network image separately)
For example: webview. loadurl ("http://www.google.com ");
2. loaddata displays Chinese webpage content (including space processing)

Webview. loaddata (urlencoder. encode (data, encoding), "text/html", "UTF-8 ");

Ii. Local File Content
For example, webview. loadurl ("file: // android_asset/test.html ");
3. Files in the APK package
1. loadurl: display the HTML and Image File webview In the APK. loadurl ("file: // android_asset/test.html"); webview. loadurl ("file: // android_asset/icon.png"); Local APK files should be stored in the assets file of the android project. The URL is file: /// android_asset,
For example, webview. loadurl ("file: // android_asset/xx.html "), IV,Use loaddatawithbaseurl to display local data and network files. I can reference some network files in the local data so that the local data can be mixed with the network files. When referencing a file, you can use a URL or a relative URL. When using a relative URL, use loaddatawithbaseurl to specify the starting address of the referenced file.
Example 1: String baseurl = "http://img6.ph.126.net"; string data = "here is a gorgeous girl <IMG src = 'hbig96b8egigbulxuwcopa =/109212290980772136.jpg '>"; webview. loaddatawithbaseurl (baseurl, data, "text/html ",
"UTF-8 ","");

See figure 2.
Note:: You must add permissions to the androidmanifest. xml file. Otherwise, the web page not available error may occur.
<Uses-Permission Android: Name = "android. Permission. Internet"/>
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;

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 ();

}

}

}

This is the use of webview components details, if you have any new use of webview components and Android development experience can send E-mail: zhousn@51cto.com, 51cto will share with you!Figure 2:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.