TheextensiveAndroidSDKallowsyoutodomanygreatthingswithparticularviewsliketheWebViewfordisplayingwebpagesonAndroidpowereddevices AndroidSDK的扩展通过使用特定的view允许你做许多事情比如WebView用来在Android手机上展示网页 AsoflatelywhileIwasexperimentingwiththeAndroidSDKIwasusingaWebViewinoneofmyactivities 最近我在体验AndroidSDK的时候在一个Activity中用到了WebView FromthatparticularWebViewIneededtoknowtheContentHeightbutalsotheContentWidth 从WebView我不但想要知道ContentHeight还想知道ContentWidth NowgettingthecontentHeightiseasylikeso: 现在的情况是获取contentHeight很easy如下 webviewgetContentHeight(); UnfortunatelygettingthecontentWidthfromaWebViewisrathermoredifficultsincethereisnotasimplemethodlike: 不幸的是从一个WebView获取contentWidth是相当困难因为SDK中没有一个像这样的方法 //THISMETHODDOESNOTEXIST! webviewgetContentWidth(); TherearewaystogetthecontentWidthoftherenderedHTMLpageandthatisthroughJavascriptIfJavascriptcangetitforyouthenyoucanalsohavetheminyourJavacodewithinyourAndroidApp 当然是有方法获取contentWidth的就是通过Javascript来获取如果你能够支持Javascript那么你就可以在你的Android程序中使用java代码来获取宽度 ByusingaJavascriptInterfacewithyourWebViewyoucanletJavascriptcommunicatewithyourAndroidAppJavacodebyinvokingmethodsonaregisteredobjectthatyoucanembedusingtheJavascriptInterface 通过在你的WebView中使用JavascriptInterface通过调用你注册的JavascriptInterface方法可以让Javascript和你的Android程序的java代码相互连通 Sohowdoesthiswork? 怎么做呢? ForaquickexampleIcreatedasimpleActivitydisplayingawebviewthatloadsawebpagewichdisplaysalogmessageandaToastmessagewiththecontentWidthwichwasdeterminedusingJavascriptNotethatthishappensAFTERthepagewasfinishedloadingbecausebeforethepageisfinishedloadingthewidthmightnotbefullyrenderedAlsokeepinmindthatifthereiscontentloadedasynchronouslythatitdoesntaffectwidths(mostlikelyonlyheightswillbeaffectedasthewidthisalmostalwaysfullydeclaredinCSSfilesunlessyouhavea%widthwebpage) 搭建一个快速的例子创建一个简单的展示webView的Activity一个LogCat消息一个Toast消息用来显示我们通过Javascript获取的宽度注意这些会在网页完全加载之后显示因为在网页加载完成之前宽度可能不能够正确的获取到同时也要注意到如果是异步加载这并不影响宽度(最多高度会受影响因为宽度总是在CSS文件中做了完全的定义除非在网页中你用了%宽度) BelowisthecodeoftheActivityMainjava: 下面的代码是Activity的代码 packagecompimmosandroidsampleswebviewcontentwidth; importandroidappActivity; importandroidosBundle; importandroidutilLog; importandroidwebkitWebView; importandroidwebkitWebViewClient; importandroidwidgetToast; publicclassMainextendsActivity{ privatefinalstaticStringLOG_TAG="WebViewContentWidth"; privatefinalActivityactivity=this; privatestaticintwebviewContentWidth=; privatestaticWebViewwebview; /**Calledwhentheactivityisfirstcreated*/ @Override publicvoidonCreate(BundlesavedInstanceState){ superonCreate(savedInstanceState); setContentView(Rlayoutmain); webview=(WebView)findViewById(Ridwebview); webviewgetSettings()setJavaScriptEnabled(true); webviewsetSaveEnabled(true); webviewaddJavascriptInterface(newJavaScriptInterface()"HTMLOUT"); webviewsetWebViewClient(newWebViewClient(){ @Override publicvoidonPageFinished(WebViewviewStringurl){ webviewloadUrl("javascript:windowHTMLOUTgetContentWidth(documentgetElementsByTagName(html)[]scrollWidth);"); } }); webviewloadUrl(";); } classJavaScriptInterface{ publicvoidgetContentWidth(Stringvalue){ if(value!=null){ webviewContentWidth=IntegerparseInt(value); Logd(LOG_TAG"Resultfromjavascript:"+webviewContentWidth); ToastmakeText(activity "ContentWidthofwebpageis:"+ webviewContentWidth+ "px"ToastLENGTH_SHORT)show(); } } } } BelowistheXMLlayoutusedwiththeActivitywichonlycontainsasimpleWebView: 下面是Activity的Layout主要就是一个简单的WebView AndroidManifestxmllayout: AndroidManifestxml代码 YoucanalsodownloadthefullsourceofAndroidApplicationWebViewContentWidth!