获取 Android 中动态创建的 WebView 的尺寸

发布于 2024-12-12 16:47:39 字数 903 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

像你 2024-12-19 16:47:39

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 a FrameLayout and getting the dimensions of the FrameLayout.

The dimensions of the WebView would always equal those of the Framelayout as long as it is the only child and both its width and height are set to match_parent

只为守护你 2024-12-19 16:47:39

您需要加载组件之后的WebView内容宽度高度 >。
是的,你这样做,但是没有 getContentWidth 方法(只有一个视图端口值),
并且 getContentHeight() 不准确

答案:WebView 子类:

  1. /* 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; //不需要

    public CustomWebView(Context context) //未使用的构造函数
    {
        超级(上下文);
        mContext = this.getContext();
    }   
    
    public CustomWebView(Context context, AttributeSet attrs) //膨胀构造函数
    {
        超级(上下文,属性);
        mContext = 上下文;
    }
    
    公共 int getContentWidth()
    {
        int ret = super.computeHorizo​​ntalScrollRange();//页面加载后工作
        原始内容宽度= ret;
        返回ret;
    }
    
    公共 int getContentHeight()
    {
        int ret = super.computeVerticalScrollRange(); //页面加载后工作
        原始内容高度= ret;
        返回ret;
    } //========== }//类 //==========
    

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:

  1. /* 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

    public CustomWebView(Context context)                     //unused constructor
    {
        super(context);
        mContext = this.getContext();
    }   
    
    public CustomWebView(Context context, AttributeSet attrs) //inflate constructor
    {
        super(context,attrs);
        mContext = context;
    }
    
    public int getContentWidth()
    {
        int ret = super.computeHorizontalScrollRange();//working after load of page
        rawContentWidth = ret;
        return ret;
    }
    
    public int getContentHeight()
    {
        int ret = super.computeVerticalScrollRange(); //working after load of page
        rawContentHeight = ret;
        return ret;
    } //========= }//class //=========
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文