偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Android:輕松獲取WebView的內(nèi)容寬度(贈源代碼)

移動開發(fā) Android
Android開發(fā)時,從WebView,我不但想要知道ContentHeight,還想知道ContentWidth。不幸的是,從一個WebView獲取contentWidth是相當(dāng)困難,因?yàn)镾DK中沒有一個像這樣的方法,所以本文為大家呈現(xiàn)了一種實(shí)用的解決此問題的方法。

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 的擴(kuò)展,通過使用特定的view,允許你做許多事情。比如,WebView,用來在Android手機(jī)上展示網(wǎng)頁。

 

As of lately while I was experimenting with the Android SDK I was using a WebView in one of my activities.

最近,我在體驗(yàn)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:

現(xiàn)在的情況是:獲取contentHeight很easy,如下:

  1. webview.getContentHeight(); 

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

不幸的是,從一個WebView獲取contentWidth是相當(dāng)困難,因?yàn)镾DK中沒有一個像這樣的方法:

  1. // THIS METHOD DOES NOT EXIST! 
  2.     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.

當(dāng)然是有方法獲取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,通過調(diào)用你注冊的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).

搭建一個快速的例子:創(chuàng)建一個簡單的展示webView的Activity,一個LogCat消息,一個Toast消息,用來顯示我們通過 Javascript獲取的寬度。注意:這些會在網(wǎng)頁完全加載之后顯示,因?yàn)樵诰W(wǎng)頁加載完成之前,寬度可能不能夠正確的獲取到。同時也要注意到,如果是異 步加載,這并不影響寬度(最多高度會受影響,因?yàn)閷挾瓤偸窃贑SS文件中做了完全的定義,除非在網(wǎng)頁中你用了100%寬度。)。

Below is the code of the Activity Main.java:

下面的代碼是Activity的代碼:

  1. 01  package com.pimmos.android.samples.webviewcontentwidth;   
  2. 02  import android.app.Activity; 
  3. 03  import android.os.Bundle; 
  4. 04  import android.util.Log; 
  5. 05  import android.webkit.WebView; 
  6. 06  import android.webkit.WebViewClient; 
  7. 07  import android.widget.Toast;   
  8. 08  public class Main extends Activity {       
  9. 09      private final static String LOG_TAG = "WebViewContentWidth"
  10. 10      private final Activity activity = this;   
  11. 11      private static int webviewContentWidth = 0
  12. 12      private static WebView webview;       
  13. 13    
  14. 14  /** Called when the activity is first created. */ 
  15. 15      @Override 
  16. 16      public void onCreate(Bundle savedInstanceState) { 
  17. 17           super.onCreate(savedInstanceState); 
  18. 18           setContentView(R.layout.main);   
  19. 19           webview = (WebView) findViewById(R.id.webview); 
  20. 20           webview.getSettings().setJavaScriptEnabled(true); 
  21. 21           webview.setSaveEnabled(true); 
  22. 22           webview.addJavascriptInterface(new JavaScriptInterface(), "HTMLOUT"); 
  23. 23           webview.setWebViewClient(new WebViewClient() { 
  24. 24               @Override 
  25. 25              public void onPageFinished(WebView view, String url) { 
  26. 26                   webview.loadUrl("javascript:window.HTMLOUT.getContentWidth(document.getElementsByTagName('html')[0].scrollWidth);"); 
  27. 27               } 
  28. 28           }); 
  29. 29           webview.loadUrl("http://www.pimmos.com/"); 
  30. 30       } 
  31. 31    
  32. 32       class JavaScriptInterface { 
  33. 33           public void getContentWidth(String value) { 
  34. 34               if (value != null) { 
  35. 35                   webviewContentWidth = Integer.parseInt(value); 
  36. 36                   Log.d(LOG_TAG, "Result from javascript: " + webviewContentWidth); 
  37. 37                   Toast.makeText(                         activity, 
  38. 38                           "ContentWidth of webpage is: " + 
  39. 39  webviewContentWidth                                 + 
  40. 40  "px", Toast.LENGTH_SHORT).show(); 
  41. 41               } 
  42. 42           } 
  43. 43       } 
  44. 44  } 

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

下面是Activity的Layout,主要就是一個簡單的WebView:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.     <LinearLayout 
  3.        xmlns:android="http://schemas.android.com/apk/res/android"    
  4.        android:orientation="vertical" 
  5.        android:layout_width="fill_parent"    
  6.        android:layout_height="fill_parent"
  7.        <WebView android:id="@+id/webview" 
  8.             android:layout_width="fill_parent" 
  9.             android:layout_height="fill_parent" /> 
  10.      </LinearLayout> 

AndroidManifest.xml layout:

AndroidManifest.xml代碼:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.     <manifest 
  3.      xmlns:android="http://schemas.android.com/apk/res/android" 
  4.         package="com.pimmos.android.samples.webviewcontentwidth" 
  5.         android:versionCode="1" android:versionName="1.0"
  6.          <application android:icon="@drawable/icon" 
  7.      android:label="@string/app_name"
  8.              <activity android:name=".Main" 
  9.      android:label="@string/app_name"
  10.                  <intent-filter> 
  11.                      <action android:name="android.intent.action.MAIN" /> 
  12.                      <category 
  13.     android:name="android.intent.category.LAUNCHER" /> 
  14.                  </intent-filter> 
  15.              </activity> 
  16.            </application> 
  17.          <uses-sdk android:minSdkVersion="7" /> 
  18.          <uses-permission android:name="android.permission.INTERNET" /> 
  19.        </manifest> 

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

你也可以到這里下載全部的源代碼:  

http://down.51cto.com/data/795941

責(zé)任編輯:閆佳明 來源: oschina
相關(guān)推薦

2009-04-03 08:28:39

2020-11-12 11:50:20

OpenHarmony

2011-05-26 14:17:16

Android 源代碼

2010-02-04 10:58:29

Android 源代碼

2014-11-06 09:31:20

Android 5.0Google

2010-10-09 11:01:31

JS

2010-02-05 18:00:18

Android源代碼

2010-03-02 10:08:28

Android源代碼

2011-11-15 10:16:41

Android 4.0Google

2013-03-26 13:42:12

Android 監(jiān)聽網(wǎng)

2017-09-18 22:55:46

GoogleAndroidRTDB

2015-01-12 10:06:02

在線客服

2010-03-05 14:38:46

Android智能手機(jī)

2009-05-30 09:19:44

AndroidGoogle移動OS

2010-03-08 15:57:27

2011-10-21 09:24:13

谷歌Android 4.0源代碼

2015-10-08 09:03:18

Android6.0源代碼

2017-04-25 12:07:51

AndroidWebViewjs

2022-01-09 20:26:14

Flink源代碼編譯

2010-03-03 17:19:48

Android
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號