【问题标题】:How to get the full height of in android WebView?如何在 android WebView 中获得完整高度?
【发布时间】:2017-09-09 16:59:08
【问题描述】:

我有一个RelativeLayoutWebView。我正在将一些生成的 html 文本加载到 WebView 中。加载数据后WebView 高度超出屏幕。现在我需要整个WebView 的高度。到目前为止,我尝试了WebView#getHeight()WebView#getBottom(),但我得到了可见内容的高度。

如何得到WebView.的总高度?

【问题讨论】:

  • 你必须使用 javascript。
  • 你能告诉我怎么做吗?

标签: android webview


【解决方案1】:

我终于得到了我的问题的答案。这是答案。

加载WebView后,我们将在WebViewClient#onPageFinished中调用,我们需要调用javascript来获取WebView的全部内容的高度

WebViewClient

/**
 * Custom web client to perform operations on WebView
 */
class WebClient extends WebViewClient {

    @Override
    public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:AndroidFunction.resize(document.body.scrollHeight)");
        }
    }
}

之后我们将在注册为JavascriptInterfaceWebInterface类中获得高度回调

/**
 * WebView interface to communicate with Javascript
 */
public class WebAppInterface {

    @JavascriptInterface
    public void resize(final float height) {
        float webViewHeight = (height * getResources().getDisplayMetrics().density);
        //webViewHeight is the actual height of the WebView in pixels as per device screen density
    }
}

【讨论】:

  • 如果需要屏幕像素的实际高度,则调整大小至关重要。非常感谢。
  • 嘿,什么是readWebView?啊- WebAppInterface 的一个实例?
  • 不,它是 webView 实例。编辑答案。谢啦。我忘了这是我的答案。 ;)
  • 如何使用这个类WebAppInterface
【解决方案2】:
private int getScale(){
    Display display = ((WindowManager) 
    getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
    int width = display.getWidth(); 
    Double val = new Double(width)/new Double(PIC_WIDTH);
    val = val * 100d;
    return val.intValue();
}

检查这个 link!了解更多信息

【讨论】:

    【解决方案3】:

    我的回答:-

    我认为您必须从 layout.xml 文件中删除填充属性

    (即)从您的 XML 文件中删除以下内容并尝试一下。

    android:paddingBottom    
    android:paddingLeft    
    android:paddingRight    
    android:paddingTop
    

    希望对你有帮助!!

    【讨论】:

      【解决方案4】:
      public class CustomWebView extends WebView{
          public CustomWebView(Context context) {
              super(context);
          }
          int ContentHeight = 0;
          public int getContentHeight(){
              if(ContentHeight==0)
                  ContentHeight = computeVerticalScrollRange();
              return ContentHeight;
          }
      }
      

      Webview 具有受保护的方法computeVerticalScrollRange()。您可以通过创建自定义方法来访问此方法。

      ((CustomWebView) webView).getContentHeight())
      

      【讨论】:

      • compute 这个词往往表示正在做一些工作,可能很繁重。
      • 我收到了ClassCastException 这个解决方案。有什么解决办法吗?
      • 没关系,我想通了。请参阅下面的完整答案。
      【解决方案5】:

      WebView 包裹在ScrollView 中,webView.getHeight() 将返回实际 WebView 内容的高度。确保您的WebView 的高度属性设置为wrap_content

      <ScrollView
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <WebView
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
          </WebView>
      </ScrollView>
      

      【讨论】:

      • 我也试过这个案例,但是如果我们加载 10 个高质量的图像,它就会发出内存错误。不建议将 WebView 保留在 ScrollView 中。
      • @shobhan 您可能会遇到 OOM,因为图像的分辨率远高于设备可以处理的分辨率,请考虑如果高分辨率图像为 50mb,则加载 10 = 250mb,然后在顶部如果您的应用程序、操作系统等经常运行在内存不足的设备上,那么您将很快杀死您的应用程序。
      • 我认为您试图解释我上述评论的一部分。 (OOM 内存溢出错误):P 谢谢。
      【解决方案6】:

      在你的 Java 类中添加 getWindow()

      @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
      
              getWindow().requestFeature(Window.FEATURE_NO_TITLE);
              getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                      WindowManager.LayoutParams.FLAG_FULLSCREEN);
              setContentView(R.layout.activity_webview);
      }
      

      【讨论】:

        猜你喜欢
        • 2014-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-25
        • 2012-03-10
        • 2013-08-28
        相关资源
        最近更新 更多