androidwebview高度
『壹』 android 開發中 怎麼用js獲取手機屏幕高度
webview.addjavascriptinterface可以調用android代碼
android可以獲得屏幕高度
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int height = dm.heightPixels//這個就是屏幕高度了。
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
這個就創立了一個介面名,叫「Android」,運行在WebView中的JS代碼可以通過這個名字調用WebAppInterface類中的showToast()方法:
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast)
{
Android.showToast(toast);
}
</script>
『貳』 android webview 怎麼放大縮小
Android:WebView如何設定支持縮放:需要對WebView和WebSettings做一下設定
webview.setVerticalScrollbarOverlay(true); //指定的垂直滾動條有疊加樣式
WebSettings settings = webview.getSettings();
settings.setUseWideViewPort(true);//設定支持viewport
settings.setLoadWithOverviewMode(true);
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);//設定支持縮放
html界面meta標簽
<metaname="viewport"content="height= [pixel_value| "device-height"] ,width= [pixel_value| "device-width"] ,initial-scale=float_value,//初始縮放minimum-scale=float_value,//最小maximum-scale=float_value,//最大user-scalable= ["yes" | "no"]//是否允許用戶對頁面縮放 "/>
例如:<meta name="viewport" content="width=device-width,user-scalable=yes initial-scale=1.0, maximum-scale=2.0">-->設定支持縮放,最大兩倍縮放
『叄』 如何設置android webview默認為高等像素密度
因為Android下瀏覽器默認的並不是實際像素,而是中像素密度。(註:Android支持三種屏幕像素密度:低像素密度,中像素密度,高像素密度),所以要設置android webview默認為高等像素密度的話,需要在js中設置如下代碼:
<metacontent='width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no,target-densitydpi=device-dpi'name='viewport'>
這裡面,target-densitydpi的功能就是指定屏幕像素密度DPI。它的參數有:
device-dpi –使用設備原本的 dpi 作為目標dpi。不會發生默認縮放。
high-dpi – 使用hdpi作為目標dpi。中等像素密度和低像素密度設備相應縮小。
medium-dpi – 使用mdpi作為目標dpi。 高像素密度設備相應放大, 像素密度設備相應縮小。這是默認的target density。
low-dpi -使用mdpi作為目標dpi。中等像素密度和高像素密度設備相應放大。
<value> – 指定一個具體的dpi值作為target dpi。這個值的范圍必須在70–400之間。
『肆』 Android開發中怎樣獲取WebView的內容寬度高度
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如下
webviewgetContentHeight();
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!
webviewgetContentWidth();
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 doesnt affect widths (most likely only heights will be affected as the width is almost always fully declared in CSS files unless you have a % width webpage)
搭建一個快速的例子創建一個簡單的展示webView的Activity一個LogCat消息一個Toast消息用來顯示我們通過 Javascript獲取的寬度注意這些會在網頁完全載入之後顯示因為在網頁載入完成之前寬度可能不能夠正確的獲取到同時也要注意到如果是異 步載入這並不影響寬度(最多高度會受影響因為寬度總是在CSS文件中做了完全的定義除非在網頁中你用了%寬度)
Below is the code of the Activity Mainjava:
下面的代碼是Activity的代碼
package ;
import androidappActivity;
import androidosBundle;
import androitilLog;
import androidwebkitWebView;
import androidwebkitWebViewClient;
import androidwidgetToast;
publicclass Main extends Activity {
privatefinalstatic String LOG_TAG = "WebViewContentWidth";
privatefinal Activity activity = this;
privatestaticint webviewContentWidth = ;
privatestatic WebView webview;
/** Called when the activity is first created */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
superonCreate(savedInstanceState);
setContentView(Rlayoutmain);
webview = (WebView) findViewById(Ridwebview);
webviewgetSettings()setJavaScriptEnabled(true);
webviewsetSaveEnabled(true);
webviewaddJavascriptInterface(new JavaScriptInterface() "HTMLOUT");
webviewsetWebViewClient(new WebViewClient() {
@Override
publicvoid onPageFinished(WebView view String url) {
webviewloadUrl("javascript:windowHTMLOUTgetContentWidth(documentgetElementsByTagName(html)[]scrollWidth);");
}
});
webviewloadUrl(";);
}
class JavaScriptInterface {
publicvoid getContentWidth(String value) {
if (value != null) {
webviewContentWidth = IntegerparseInt(value);
Logd(LOG_TAG "Result from javascript: " + webviewContentWidth);
ToastmakeText( activity
"ContentWidth of webpage is: " +
webviewContentWidth +
"px" ToastLENGTH_SHORT)show();
}
}
}
}
Below is the XML layout used with the Activity wich only contains a simple WebView:
下面是Activity的Layout主要就是一個簡單的WebView
<?xml version="" encoding="utf"?>
<LinearLayout
xmlns: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>
AndroidManifestxml layout:
AndroidManifestxml代碼
<?xml version="" encoding="utf"?>
<manifest
xmlns:android=";
package=""
android:versionCode="" android:versionName="">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name="Main"
android:label="@string/app_name">
<intentfilter>
<action android:name="androidintentactionMAIN" />
<category
android:name="androidintentcategoryLAUNCHER" />
</intentfilter>
</activity>
</application>
<usessdk android:minSdkVersion="" />
<usespermission android:name="androidpermissionINTERNET" />
</manifest>
You can also download the full source of Android Application WebViewContentWidth!
『伍』 SVG圖片自適應Android webview大小
WebSettings ws = tv.getSettings();
//html的圖片就會以單列顯示就不會變形佔了別的位置
ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
//讓縮放顯示的最小值為起始
webView.setInitialScale(5);
// 設置支持縮放
webSettings.setSupportZoom(true);
// 設置縮放工具的顯示
webSettings.setBuiltInZoomControls(true);
『陸』 Android中如何在代碼中設置View的寬和高
LayoutParams params = mWebViewHeader.getLayoutParams(); params.height = height; params.width = LayoutParams.FILL_PARENT; mWebViewHeader.setLayoutParams(params);