当前位置:首页 » 安卓系统 » androidwebview高度

androidwebview高度

发布时间: 2023-07-03 21:04:06

‘壹’ 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);

热点内容
西安java学习 发布:2025-02-06 19:15:44 浏览:621
微信电影源码网站 发布:2025-02-06 18:55:21 浏览:933
本地建mysql数据库 发布:2025-02-06 18:54:23 浏览:761
屏幕看不清了如何输密码 发布:2025-02-06 18:51:14 浏览:332
手机开脚本买个什么配置的 发布:2025-02-06 18:45:59 浏览:111
python代码输入 发布:2025-02-06 18:32:35 浏览:562
易语言上传ftp文件夹 发布:2025-02-06 18:31:09 浏览:73
仿qq源码java 发布:2025-02-06 18:24:06 浏览:424
阿里云访问mysql数据库 发布:2025-02-06 18:17:57 浏览:789
原神游戏服务器ip 发布:2025-02-06 17:54:23 浏览:811