转至:http://xwangly.iteye.com/blog/1846387


使用WebView加载HTML资源

 

最近的项目需要做一个如同微信中的腾讯新闻的功能,如下图所示:

 

直接上关键代码:

布局:

Xml代码   
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@color/contact_bg_gray"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/time"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_gravity="center_horizontal"  
  14.         android:layout_marginTop="10dip"  
  15.         android:background="@drawable/big_corner_time"  
  16.         android:gravity="center"  
  17.         android:paddingBottom="2dip"  
  18.         android:paddingLeft="10dip"  
  19.         android:paddingRight="10dip"  
  20.         android:paddingTop="2dip"  
  21.         android:text="9:32"  
  22.         android:textColor="@android:color/white"  
  23.         android:textSize="18sp" />  
  24.   
  25.     <WebView  
  26.         android:id="@+id/webView1"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_below="@+id/time"  
  30.         android:layout_centerHorizontal="true"  
  31.         android:layout_marginTop="8dip"  
  32.         android:background="@color/contact_bg_gray"  
  33.         android:layerType="software" />  
  34.   
  35. RelativeLayout>  

 颜色请自行替换。

 

Java代码   
  1.         mWebView = (WebView) findViewById(R.id.webView1);  
  2. //        holder.mWebView.loadUrl("http://www.baidu.com/");  
  3.         WebSettings settings = mWebView.getSettings();  
  4. //  
  5.         settings.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);  
  6.         settings.setAppCacheEnabled(true);  
  7.         int width = FAMobileApp.getDeviceWidth() - (int) (16 * FAMobileApp.getDensity());  
  8.         int scaleInPercent = width * 100 / 322;  
  9.         System.out.println("scaleInPercent:"+scaleInPercent);  
  10.         mWebView.setInitialScale(scaleInPercent);  
  11.   
  12.     mWebView.setBackgroundColor(mContext.getResources().getColor(  
  13.                 R.color.contact_bg_gray));  
  14.         mWebView.setWebViewClient(new WebViewClient() {  
  15.   
  16.             @Override  
  17.             public boolean shouldOverrideUrlLoading(WebView view, String url) {  
  18.                 System.out.println("shouldOverrideUrlLoading:"+url);  
  19.                 //此处可作转向操作  
  20.                 view.loadUrl(url);  
  21.                 return true;  
  22.             }  
  23.             @Override  
  24.             public void onLoadResource(WebView view, String url) {  
  25.                 System.out.println("onLoadResource:"+url);  
  26.             }  
  27.             @Override  
  28.             public WebResourceResponse shouldInterceptRequest(WebView view,  
  29.                     String url) {  
  30.                 System.out.println("shouldInterceptRequest:"+url);  
  31.                 //TODO Here we can replace the url  
  32.                 if (url.endsWith("cj_all_picture.png")) {  
  33. //                    try {  
  34. //                        InputStream is = mContext.getResources().getAssets().open("cj_all_tab_bg.png");  
  35. //                        WebResourceResponse response = new WebResourceResponse("image/gif", "utf-8", is);  
  36. //                        return response;  
  37. //                    } catch (IOException e) {  
  38. //                        // TODO Auto-generated catch block  
  39. //                        e.printStackTrace();  
  40. //                    }  
  41.                 }  
  42.                 return null;  
  43.             }  
  44.         });  
  45.         load(holder.mWebView);  
  46.   
  47.     private void load(WebView mWebView) {  
  48.   
  49.   
  50.         try {  
  51.             String basePath = "/data/data/com.xxx.xx/test";  
  52.             StringBuilder content = new StringBuilder();  
  53.             String path = basePath + "cjsd.html";  
  54.             fis = new FileInputStream(path);  
  55.             byte[] buffer = new byte[1024];  
  56.             int len = 0;  
  57.             while ((len = fis.read(buffer)) != -1) {  
  58.                 content.append(new String(buffer, 0, len, "gbk"));  
  59.             }  
  60.             // System.out.println(content);  
  61.             mWebView.getSettings().setDefaultTextEncodingName("utf-8");  
  62.             String baseUrl = "file://" + basePath;  
  63.             mWebView.loadDataWithBaseURL(baseUrl, content.toString(), "text/html""utf-8",  
  64.                     null);  
  65.         } catch (FileNotFoundException e) {  
  66.             // TODO Auto-generated catch block  
  67.             e.printStackTrace();  
  68.         } catch (IOException e) {  
  69.             // TODO Auto-generated catch block  
  70.             e.printStackTrace();  
  71.         }  
  72.   
  73.   
  74.     }  

 

使用的cjsc.html如下

Html代码   
  1. >  
  2.   
  3. <head>  
  4. <style>  
  5. 更多相关文章

    1. attrs.xml文件中属性类型format值的格式
    2. Android中Window添加View的底层原理
    3. Android(安卓)接入阿里反馈 (基础版)
    4. Android使用FileObserver对sdcard文件或文件夹监控
    5. android studio 升级到3.0.1 原有项目运行在android 4.4停止运行
    6. Unity 实现Android不锁屏
    7. Android(安卓)Camera子系统之进程/文件View
    8. android 中的Main调试方法
    9. 2.7-2 Android(安卓)studio gradle 文件 配置完整版

    随机推荐

    1. Android电源管理之一:基础概览
    2. Android自定义属性时format选项( )
    3. 【Android(安卓)应用开发】Activity 状态
    4. Android之用HttpURLConnection参数以XML
    5. android 中按键响应实例
    6. android http UrlEncode 中文乱码等问题
    7. android实现分享功能
    8. Android开发--在Eclipse中使用android.su
    9. Android分包机制个人总结
    10. android实现音乐播放器(进度条)