content view width and height

2019-01-28 08:56发布

问题:

I am using below steps to know the width and height of contentview on my display screen

ContentViewWidth = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getWidth();
ContentViewHeight = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getHeight();

but this method doesn't work inside onCreate or onResume and if I am using these steps inside onWindowFocusChanged I get force close error on my phone

Please help me how can I get the content view of my display screen. Content view is excluding statusbar and titlebar dimensions

回答1:

I use the following technique - post a runnable from onCreate() that will be executed when the view has been created:

    contentView = findViewById(android.R.id.content);
    contentView.post(new Runnable()
    {
        public void run()
        {
            contentHeight = contentView.getHeight();
        }
    });

This code will run on the main UI thread, after onCreate() has finished.


If you are trying to get the height of the content view itself, you need to subtract the padding from the height - this is because Android 2.x lays out the views differently to 4.x.

contentHeight = contentView.getHeight() - contentView.getTopPadding();


回答2:

Here's what I did in my activity to figure out the content layout of a particular view child that I knew stretched end-to-end on the screen (a webview in this case):

First, inherit from OnGlobalLayoutListener:

public class ContainerActivity extends Activity implements ViewTreeObserver.OnGlobalLayoutListener {
...

Next, implement onGlobalLayout method of listener's interface (note I do some pixel calcs that pertain to figuring out the medium-zoom dip :

@Override
public void onGlobalLayout() {
    int nH = this.mWebView.getHeight();
    int nW = this.mWebView.getWidth();

    if (nH > 0 && nW > 0)
    {
        DisplayMetrics oMetrics = new DisplayMetrics();

        this.getWindowManager().getDefaultDisplay().getMetrics(oMetrics); 

        nH = (nH * DisplayMetrics.DENSITY_DEFAULT) / oMetrics.densityDpi;
        nW = (nW * DisplayMetrics.DENSITY_DEFAULT) / oMetrics.densityDpi;

        // do something with nH and nW                  

        this.mWebView.getViewTreeObserver().removeGlobalOnLayoutListener
            ( this
            );

            ...         
    }
}

Finally, make sure to tell the view element (mWebView in this case) inside onCreate() that you're listening to layout events:

this.setContentView(this.mWebView = new WebView(this));

this.mWebView.getViewTreeObserver().addOnGlobalLayoutListener
    ( this
    );

This approach works for older android versions. I believe ics+ has a layout listener for each view you can bind to, and as such, would be used in a similar way.



回答3:

Rect rectangle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int contentHeight = window.findViewById(Window.ID_ANDROID_CONTENT).getHeight();
int contentWidth = window.findViewById(Window.ID_ANDROID_CONTENT).getWidth();