How to get width and height of a Webview in android

后端 未结 12 1119
长发绾君心
长发绾君心 2021-01-04 17:13

I want to determine the width and the height of the WebView. I have already tried it using:

webView.getWidth();
webView.getHeight();
         


        
12条回答
  •  感情败类
    2021-01-04 17:41

    Hmmm - I looked through the WevView source and I can see that internally they are using View#getWidth and View#getHeight here's one of the WebView's private methods:

    /*
     * Return the width of the view where the content of WebView should render
     * to.
     */
    private int getViewWidth() {
        if (!isVerticalScrollBarEnabled() || mOverlayVerticalScrollbar) {
            return getWidth();
        } else {
            return getWidth() - getVerticalScrollbarWidth();
        }
    }
    

    As noted in the comments you have to make sure you are measuring it after you assign the activity layout e.g. setContentView(R.layout.mylayout);

提交回复
热议问题