[Android Learning Series 2] loading local js,js,html files with WebView write interface

Source: Internet
Author: User

Using jquery mobile as an example

1. Drag a webview into the Android interface and add an Internet permission

<uses-SDK      android:minsdkversion= "8"      android:targetsdkversion= "/>"  < Uses-permission android:name= "Android.permission.INTERNET"/>    <application              .............................  
View Code

2. Put the js,css,html resource file in the assets directory

3. Introduce the corresponding path in assert when writing local HTML

<!DOCTYPE HTML>  <Head>  <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" />  <Metaname= "Viewport"content= "Width=device-width, initial-scale=1">   <title>Title</title>  <Linkrel= "stylesheet"type= "Text/css"href= "File:///android_asset/css/jquery.mobile-1.4.2.min.css">  <Scriptsrc= "File:///android_asset/js/jquery-1.7.1.min.js"></Script>  <Scriptsrc= "File:///android_asset/js/jquery.mobile-1.4.2.min.js"></Script>  </Head>  <Body>         <DivData-role= "page">         <DivData-role= "header">          <H1>My Title</H1>         </Div>                <DivData-role= "Content">          <ulData-role= "ListView"Data-inset= "true" >              <Li><ahref="#">Acura</a></Li>              <Li><ahref="#">Audi</a></Li>              <Li><ahref="#">BMW</a></Li>              <Li><ahref="#">Cadillac</a></Li>              <Li><ahref="#">Ferrari</a></Li>          </ul>         </Div>     </Div><!--/page -    </Body>  </HTML>  
View Code

4. Accessing the page in the code

 PackageCom.example.asd_webview;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;Importandroid.webkit.WebSettings;Importandroid.webkit.WebSettings.RenderPriority;ImportAndroid.webkit.WebView; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); WebView WebView=(WebView) Findviewbyid (R.ID.WEBVIEW1); WebSettings Wv_setttig=webview.getsettings (); Wv_setttig.setjavascriptenabled (true); //wv_setttig.setrenderpriority (renderpriority.high);String URL= "File:///android_asset/index.html";            Webview.loadurl (URL); } @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; }}
View Code

5. The final effect is as follows:

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.