日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 获取WebView的内容宽度高度

發布時間:2024/4/17 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 获取WebView的内容宽度高度 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉自: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的内容宽度高度的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。