加入收藏 | 设为首页 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > 本地

安卓如何使用WebView组件加载HTML代码

时间:2024-03-08 02:06:38  来源:http://www.gzrxw.net  作者:admin

1)修改新建项目的res\Iayout目录下的布局文件main.xml,将默认添加的TextView组件删除,然后添加一个WebView组件

2)在MainActivity的onCreate()方法中,首先获取布局管理器中添加的Web View组件,然后创建一个字符串构建器,将要显示的HTML代码放置在该构建器中,最后应用IoadDataWithBaseURLO方法加载构建器中的HTML代码

android客户端常会调用到html页面,给webview页面适配android凌乱的设备带来很大的困难。

可以找到的方法是通过ZoomDensity.setDefaultZoom根据分辨率480宽度为基准缩放。

不过ZoomDensity.setDefaultZoom在2.0以下的平台是无法调用的,需要自己反射调用。

即使是ZoomDensity.setDefaultZoom设置了缩放,但还是会在很多设备无效。经过摸索还需做一些修改:

1,页面head添加

     <meta name=viewport content=width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no />

2,设置字体也按480基准缩放。

下面是代码:

Java代码 

private void setZoom(WebSettings webSettings) {  int screenDensity = getResources().getDisplayMetrics().densityDpi;  String zd = FAR;  switch (screenDensity) {  case DisplayMetrics.DENSITY_LOW:      zd = CLOSE;      break;   case DisplayMetrics.DENSITY_MEDIUM:      zd = MEDIUM;      break;  }  Class<?> zoomDensityClass = null;  Enum<?> zoomDensity = null;   try {      if (zoomDensityClass == null) {      zoomDensityClass = Class.forName(android.webkit.WebSettings$ZoomDensity);      }      if (zoomDensity == null) {      zoomDensity = (Enum<?>) Enum.valueOf((Class) zoomDensityClass,zd);      }       Method method = WebSettings.class.getDeclaredMethod( setDefaultZoom, new Class<?>[] { zoomDensityClass });      if(method!=null){      method.invoke(webSettings, zoomDensity);      }            method = WebSettings.class.getDeclaredMethod( setTextZoom, new Class<?>[] { int.class });      if(method!=null){      method.invoke(webSettings, 100 * getWindowManager().getDefaultDisplay().getWidth() / 480);      }  } catch (Exception e) {      Log.e(TAG, e.getMessage());      return;  }     }

Android的webview加载本地html、本apk内html和远程URL

Android的webview加载本地html、本apk内html和远程URL

android客户端常会调用到html页面,给webview页面适配android凌乱的设备带来很大的困难。

可以找到的方法是通过ZoomDensity.setDefaultZoom根据分辨率480宽度为基准缩放。

不过ZoomDensity.setDefaultZoom在2.0以下的平台是无法调用的,需要自己反射调用。

即使是ZoomDensity.setDefaultZoom设置了缩放,但还是会在很多设备无效。经过摸索还需做一些修改:

1,页面head添加

     <meta name=viewport content=width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no />

2,设置字体也按480基准缩放。

下面是代码:

Java代码 

private void setZoom(WebSettings webSettings) {  int screenDensity = getResources().getDisplayMetrics().densityDpi;  String zd = FAR;  switch (screenDensity) {  case DisplayMetrics.DENSITY_LOW:      zd = CLOSE;      break;   case DisplayMetrics.DENSITY_MEDIUM:      zd = MEDIUM;      break;  }  Class<?> zoomDensityClass = null;  Enum<?> zoomDensity = null;   try {      if (zoomDensityClass == null) {      zoomDensityClass = Class.forName(android.webkit.WebSettings$ZoomDensity);      }      if (zoomDensity == null) {      zoomDensity = (Enum<?>) Enum.valueOf((Class) zoomDensityClass,zd);      }       Method method = WebSettings.class.getDeclaredMethod( setDefaultZoom, new Class<?>[] { zoomDensityClass });      if(method!=null){      method.invoke(webSettings, zoomDensity);      }            method = WebSettings.class.getDeclaredMethod( setTextZoom, new Class<?>[] { int.class });      if(method!=null){      method.invoke(webSettings, 100 * getWindowManager().getDefaultDisplay().getWidth() / 480);      }  } catch (Exception e) {      Log.e(TAG, e.getMessage());      return;  }     }

来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
相关文章
    无相关信息
栏目更新
栏目热门