转自:http://www.android100.org/html/201311/19/4804.html

Android开发时,从WebView,我不但想要知道ContentHeight,还想知道ContentWidth。不幸的是,从一个 WebView获取contentWidth是相当困难,因为SDK中没有一个像这样的方法,所以本文为大家呈现了一种实用的解决此问题的方法。

The extensive Android SDK allows you to do many great things with particular views like the WebView for displaying webpages on Android powered devices.
Android SDK 的扩展,通过使用特定的view,允许你做许多事情。比如,WebView,用来在Android手机上展示网页。
 
As of lately while I was experimenting with the Android SDK I was using a WebView in one of my activities.
最近,我在体验Android SDK的时候,在一个Activity中用到了WebView。
From that particular WebView I needed to know the ContentHeight but also the ContentWidth.
从WebView,我不但想要知道ContentHeight,还想知道ContentWidth。
Now getting the contentHeight is easy like so:
现在的情况是:获取contentHeight很easy,如下:

<span style="font-size:18px;">webview.getContentHeight(); </span>

Unfortunately getting the contentWidth from a WebView is rather more difficult, since there is not a simple method like:
不幸的是,从一个WebView获取contentWidth是相当困难,因为SDK中没有一个像这样的方法:

<span style="font-size:18px;">// THIS METHOD DOES NOT EXIST!webview.getContentWidth(); </span>

There are ways to get the contentWidth of the rendered HTML page and that is through Javascript. If Javascript can get it for you, then you can also have them in your Java code within your Android App.
当然是有方法获取contentWidth的,就是通过Javascript来获取。如果你能够支持Javascript,那么你就可以在你的 Android 程序中,使用java代码来获取宽度。
By using a JavascriptInterface with your WebView you can let Javascript communicate with your Android App Java code by invoking methods on a registered object that you can embed using the JavascriptInterface.
通过在你的WebView中使用JavascriptInterface,通过调用你注册的JavascriptInterface方法,可以让 Javascript和你的Android程序的java代码相互连通。
So how does this work?
怎么做呢?
For a quick example I created a simple Activity displaying a webview that loads a webpage wich displays a log message and a Toast message with the contentWidth wich was determined using Javascript. Note that this happens AFTER the page was finished loading, because before the page is finished loading the width might not be fully rendered. Also keep in mind that if there is content loaded asynchronously that it doesn't affect widths (most likely only heights will be affected as the width is almost always fully declared in CSS files unless you have a 100% width webpage).
搭建一个快速的例子:创建一个简单的展示webView的Activity,一个LogCat消息,一个Toast消息,用来显示我们通过 Javascript获取的宽度。注意:这些会在网页完全加载之后显示,因为在网页加载完成之前,宽度可能不能够正确的获取到。同时也要注意到,如果是异 步加载,这并不影响宽度(最多高度会受影响,因为宽度总是在CSS文件中做了完全的定义,除非在网页中你用了100%宽度。)。
Below is the code of the Activity Main.java:
下面的代码是Activity的代码:

<span style="font-size:18px;">package com.pimmos.android.samples.webviewcontentwidth;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class Main extends Activity {       private final static String LOG_TAG = "WebViewContentWidth"; private final Activity activity = this;   private static int webviewContentWidth = 0; private static WebView webview;       /** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);   webview = (WebView) findViewById(R.id.webview); webview.getSettings().setJavaScriptEnabled(true); webview.setSaveEnabled(true); webview.addJavascriptInterface(new JavaScriptInterface(), "HTMLOUT"); webview.setWebViewClient(new WebViewClient() { @Overridepublic void onPageFinished(WebView view, String url) { webview.loadUrl("javascript:window.HTMLOUT.getContentWidth(document.getElementsByTagName('html')[0].scrollWidth);"); } }); webview.loadUrl("http://www.pimmos.com/"); } class JavaScriptInterface { public void getContentWidth(String value) { if (value != null) { webviewContentWidth = Integer.parseInt(value); Log.d(LOG_TAG, "Result from javascript: " + webviewContentWidth); Toast.makeText(                         activity, "ContentWidth of webpage is: " + webviewContentWidth                                 + "px", Toast.LENGTH_SHORT).show(); } } }
} </span>

Below is the XML layout used with the Activity wich only contains a simple WebView:
下面是Activity的Layout,主要就是一个简单的WebView:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"android:layout_width="fill_parent"    android:layout_height="fill_parent"> <WebView android:id="@+id/webview"android:layout_width="fill_parent"android:layout_height="fill_parent" /> </LinearLayout> </span>

AndroidManifest.xml layout:
AndroidManifest.xml代码:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.pimmos.android.samples.webviewcontentwidth"android:versionCode="1"android:versionName="1.0" ><applicationandroid:icon="@drawable/icon"android:label="@string/app_name" ><activityandroid:name=".Main"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application><uses-sdk android:minSdkVersion="7" /><uses-permission android:name="android.permission.INTERNET" /></manifest></span>

Android 获取WebView的内容宽度高度相关推荐

  1. android webview 截图,Android获取webView快照与屏幕截屏的方法 -电脑资料

    前段时间做的一个书店项目其阅读模块中用到了WebView + js,今天把WebView这块用到的几个特性记录下, 其主要用到了webView的快照与屏幕的截屏.部分代码如下: [html] /** ...

  2. android 获取webView高度,设置webView高度

    1.注入获取webView高度的js方法 webView.setWebViewClient(new WebViewClient() {@Overridepublic boolean shouldOve ...

  3. Android获取设备状态栏status bar高度的正确姿势

    Android获取设备状态栏高度的正确姿势 正确代码方式: int height = 0;int resourceId = getApplicationContext().getResources() ...

  4. Android获取虚拟导航键的高度

    自从有了全面屏,就有了虚拟按键,我们该如何获取 屏幕的真实高度 以及 虚拟键的高度 呢? 之前我们使用的都是下面的方法,但有一个问题就是,在全面屏中,它获取到的高度是不包含下面导航键的高度的: pub ...

  5. jquery获取隐藏元素的宽度高度

    $('.info').show(50,function(){var w = $('.info').outerWidth();console.log(w); }); 注意: show的第一个参数不能为0 ...

  6. Js获取字符串的显示宽度/高度

    重点: 1.在H5页面,文字大小单位为rem 2.不同的font-family,文字的宽度不一样 3.文字宽度同时受font-size和font-family影响 思路: 在页面动态创建一个节点,设置 ...

  7. vue获取屏幕的实时 宽度 / 高度

    mounted () {const that = this;window.onresize = () => {return (() => {window.screenWidth = doc ...

  8. 使用Javascript获取图片坐标以及宽度高度的方法

    首先说下获取图片宽度与长度的问题: <script type="text/javascript">function lpimgClick(img,event){var ...

  9. Android 获取彩信文本内容及 发送时间 发送人

    final String SMS_URI_MMS = "content://mms"; //此处查询的表是pdu表 ContentResolver MMScr = context. ...

最新文章

  1. PostgreSQL SQL 语言:并行查询
  2. mybaits十一:使用association分步查询
  3. thinkphp3.2与phpexcel带图片生成 完美案例
  4. mysql group by having count_mysql中count(), group by, order by使用详解
  5. 最全!最完整的求first集和follow的代码!!!编译原理 FIRST集和FOLLOW集的求法 代码,程序,实验报告
  6. 教育管理系统——android家长客户端
  7. 如何在Spring Boot应用程序中使用配置文件
  8. 安装安全类软件进行了android签名漏洞修补,魅族MX3怎么升级固件体验最新比较稳定的版本...
  9. 怎样取消连续包月自动续费_苹果手机连续包月会员怎么取消 设置iPhone解除应用自动续费...
  10. 新浪微博API使用方法
  11. 论文笔记_CV_AD_3D Reconstruction using a Sparse Laser Scanner and a Single Camera for Outdoor Autonomous
  12. SpringBoot整合Mybatis详细教程
  13. linux 怎么查看谁登过这台服务器,如何查看linux服务器是否被入侵
  14. ACT托管的CRM软件开发您的业务
  15. 华为服务器进入系统怎么退出安全模式,华为手机怎么进入和退出安全模式?华为手机进入安全模式按哪个键...
  16. 高博SLAM基础课第四讲——非线性优化
  17. 用python画一只小狗
  18. 使用Guardium和WebSphere Application Server监视应用程序用户的数据库活动
  19. Viewstate verification failed 解决办法
  20. resent = msg.get_all('Resent-Date') AttributeError: 'str' object has no attribute 'get_all'

热门文章

  1. cmd后台运行exe_windows 十大实用“运行”命令
  2. python中整数类型有—3_Python3 基本数据类型(3)
  3. 渲染好的页面怎么转为图片_图片导成PDF后页面大小不同怎么办
  4. 计算机启动进入不了桌面图标,电脑开机只显示桌面背景而无桌面图标怎么办
  5. 有关于idea快捷键冲突的问题
  6. php 查看当前字符编码,PHP检测当前字符编码并转码
  7. 什么是Boot Loader
  8. 2018年长沙理工大学第十三届程序设计竞赛 G-逃离迷宫
  9. 计算机辅助工艺设计张胜文,计算机辅助工装设计.ppt
  10. c语言结构体的位操作,C语言之路---结构体、位运算及预处理命令