content view width and height

后端 未结 3 707
野的像风
野的像风 2020-12-21 07:14

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

ContentViewWidth = getWindow().findViewById(Window.ID_ANDROID_CONTENT         


        
相关标签:
3条回答
  • 2020-12-21 07:35

    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.

    0 讨论(0)
  • 2020-12-21 07:40

    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();
    
    0 讨论(0)
  • 2020-12-21 07:41
    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();
    
    0 讨论(0)
提交回复
热议问题