获取 Android 中动态创建的 WebView 的尺寸
我在 android 的 webView 中嵌入了一个 flash 媒体播放器,我需要获取 webView 的尺寸来设置媒体播放器来匹配。如果我在调用中对播放器的像素尺寸进行硬编码,它就可以工作,但我希望它根据 webView 的尺寸进行调整,它使用 android:width=fill_parent 和 android:height=wrap_content ,并根据屏幕方向。
根据我读到的内容,我尝试等待 onCreate() 完成并在 onStart() 和 onResume() 方法中调用 getHeight() 和 getWidth() ,但它们仍然返回 0。这是我调用的方法在 onStart() 中
private void playVideo(String path){
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setPluginsEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
int videoHeight = webView.getHeight();
int videoWidth = webView.getWidth();
webView.loadUrl("*my url here*?video="+path+
".mp4&width="+videoWidth+"&height="+videoHeight);
}
由于某种原因,无论我在 onCreate()、onStart() 还是 onResume() 中调用该方法,我总是得到 0 作为高度和宽度。我有什么想法可以解决这个问题吗?
I'm embedding a flash media player in a webView in android, and I need to get the dimensions of the webView to set the media player to match. If I hard-code the pixel dimensions of the player in the call, it works, but I want it to adjust based on the dimensions of the webView, which uses android:width=fill_parent and android:height=wrap_content, and change depending on screen orientation.
From what I've read, I've tried waiting for onCreate() to finish and calling getHeight() and getWidth() in the onStart() and onResume() methods, but they both still return 0. Here's the method I call in onStart()
private void playVideo(String path){
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setPluginsEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
int videoHeight = webView.getHeight();
int videoWidth = webView.getWidth();
webView.loadUrl("*my url here*?video="+path+
".mp4&width="+videoWidth+"&height="+videoHeight);
}
For some reason I always get 0 as the height and width, whether I call the method in onCreate(), onStart() or onResume(). Any ideas how I can fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WebView
是一个很重的组件,在您尝试获取其宽度和高度之前,它可能尚未完全呈现在屏幕上。一个简单的解决方法是将
WebView
放入另一个容器(最好是FrameLayout
)中,并获取FrameLayout
的尺寸。只要
WebView
是唯一的子级且其宽度和高度都设置为match_parent
,它的尺寸就始终等于Framelayout
的尺寸>The
WebView
is a heavy component and its possible that it has not been fully rendered on the screen before you tried to get its width and height.A simple work around would involve putting the
WebView
into another container preferably aFrameLayout
and getting the dimensions of theFrameLayout
.The dimensions of the
WebView
would always equal those of theFramelayout
as long as it is the only child and both its width and height are set tomatch_parent
您需要加载组件之后的WebView内容的宽度和高度 >。
是的,你这样做,但是没有 getContentWidth 方法(只有一个视图端口值),
并且 getContentHeight() 不准确!
答案:WebView 子类:
/* Jon Goodwin
*/ package com.example.html2pdf;//你的包
导入android.content.Context;导入 android.util.AttributeSet;
导入 android.webkit.WebView;
类 CustomWebView 扩展 WebView {
公共 int rawContentWidth = 0; //不需要的
公共 int rawContentHeight = 0; //不需要的
上下文 mContext = null; //不需要
You need the width and height of the WebView CONTENTS, after you loaded your component.
YES you do, but there is no getContentWidth method (only a view port value),
AND the getContentHeight() is inaccurate !
Answer: sub-class WebView:
/* Jon Goodwin
*/ package com.example.html2pdf;//your package
import android.content.Context; import android.util.AttributeSet;
import android.webkit.WebView;
class CustomWebView extends WebView {
public int rawContentWidth = 0; //unneeded
public int rawContentHeight = 0; //unneeded
Context mContext = null; //unneeded