android 获取源程序量,Android:轻松获取WebView的内容宽度(赠源代码)

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,如下:

webview.getContentHeight();

Unfortunately getting the contentWidth from a WebView is rather more difficult, since there is not a simple method like:

不幸的是,从一个WebView获取contentWidth是相当困难,因为SDK中没有一个像这样的方法:

// THIS METHOD DOES NOT EXIST!

webview.getContentWidth();

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的代码:

01packagecom.pimmos.android.samples.webviewcontentwidth;

02importandroid.app.Activity;

03importandroid.os.Bundle;

04importandroid.util.Log;

05importandroid.webkit.WebView;

06importandroid.webkit.WebViewClient;

07importandroid.widget.Toast;

08publicclassMainextendsActivity {

09privatefinalstaticString LOG_TAG ="WebViewContentWidth";

10privatefinalActivity activity =this;

11privatestaticintwebviewContentWidth =0;

12privatestaticWebView webview;

13

14/** Called when the activity is first created. */

15@Override

16publicvoidonCreate(Bundle savedInstanceState) {

17super.onCreate(savedInstanceState);

18setContentView(R.layout.main);

19webview = (WebView) findViewById(R.id.webview);

20webview.getSettings().setJavaScriptEnabled(true);

21webview.setSaveEnabled(true);

22webview.addJavascriptInterface(newJavaScriptInterface(),"HTMLOUT");

23webview.setWebViewClient(newWebViewClient() {

24@Override

25publicvoidonPageFinished(WebView view, String url) {

26webview.loadUrl("javascript:window.HTMLOUT.getContentWidth(document.getElementsByTagName('html')[0].scrollWidth);");

27}

28});

29webview.loadUrl("http://www.pimmos.com/");

30}

31

32classJavaScriptInterface {

33publicvoidgetContentWidth(String value) {

34if(value !=null) {

35webviewContentWidth = Integer.parseInt(value);

36Log.d(LOG_TAG,"Result from javascript: "+ webviewContentWidth);

37Toast.makeText(                         activity,

38"ContentWidth of webpage is: "+

39webviewContentWidth                                 +

40"px", Toast.LENGTH_SHORT).show();

41}

42}

43}

44}

Below is the XML layout used with the Activity wich only contains a simple WebView:

下面是Activity的Layout,主要就是一个简单的WebView:

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_width="fill_parent"

android:layout_height="fill_parent"/>

AndroidManifest.xml layout:

AndroidManifest.xml代码:

xmlns:android="http://schemas.android.com/apk/res/android"

package="com.pimmos.android.samples.webviewcontentwidth"

android:versionCode="1"android:versionName="1.0">

android:label="@string/app_name">

android:label="@string/app_name">

android:name="android.intent.category.LAUNCHER"/>

You can also download the full source of Android Application - WebViewContentWidth!

你也可以到这里下载全部的源代码:

【编辑推荐】

【责任编辑:闫佳明 TEL:(010)68476606】

点赞 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值