How to retrieve the dimensions of a view?

前端 未结 16 1529
小蘑菇
小蘑菇 2020-11-22 01:09

I have a view made up of TableLayout, TableRow and TextView. I want it to look like a grid. I need to get the height and width of this grid. The methods

相关标签:
16条回答
  • 2020-11-22 01:16

    Are you trying to get sizes in a constructor, or any other method that is run BEFORE you get the actual picture?

    You won't be getting any dimensions before all components are actually measured (since your xml doesn't know about your display size, parents positions and whatever)

    Try getting values after onSizeChanged() (though it can be called with zero), or just simply waiting when you'll get an actual image.

    0 讨论(0)
  • 2020-11-22 01:17

    I'll just add an alternative solution, override your activity's onWindowFocusChanged method and you will be able to get the values of getHeight(), getWidth() from there.

    @Override
    public void onWindowFocusChanged (boolean hasFocus) {
            // the height will be set at this point
            int height = myEverySoTallView.getMeasuredHeight(); 
    }
    
    0 讨论(0)
  • 2020-11-22 01:20

    As F.X. mentioned, you can use an OnLayoutChangeListener to the view that you want to track itself

    view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            // Make changes
        }
    });
    

    You can remove the listener in the callback if you only want the initial layout.

    0 讨论(0)
  • 2020-11-22 01:21

    I believe the OP is long gone, but in case this answer is able to help future searchers, I thought I'd post a solution that I have found. I have added this code into my onCreate() method:

    EDITED: 07/05/11 to include code from comments:

    final TextView tv = (TextView)findViewById(R.id.image_test);
    ViewTreeObserver vto = tv.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    
        @Override
        public void onGlobalLayout() {
            LayerDrawable ld = (LayerDrawable)tv.getBackground();
            ld.setLayerInset(1, 0, tv.getHeight() / 2, 0, 0);
            ViewTreeObserver obs = tv.getViewTreeObserver();
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                obs.removeOnGlobalLayoutListener(this);
            } else {
                obs.removeGlobalOnLayoutListener(this);
            }
        }
    
    });
    

    First I get a final reference to my TextView (to access in the onGlobalLayout() method). Next, I get the ViewTreeObserver from my TextView, and add an OnGlobalLayoutListener, overriding onGLobalLayout (there does not seem to be a superclass method to invoke here...) and adding my code which requires knowing the measurements of the view into this listener. All works as expected for me, so I hope that this is able to help.

    0 讨论(0)
  • 2020-11-22 01:23

    Height and width are zero because view has not been created by the time you are requesting it's height and width . One simplest solution is

    view.post(new Runnable() {
        @Override
        public void run() {
            view.getHeight(); //height is ready
            view.getWidth(); //width is ready
        }
    });
    

    This method is good as compared to other methods as it is short and crisp.

    0 讨论(0)
  • 2020-11-22 01:23

    Use getMeasuredWidth() and getMeasuredHeight() for your view.

    Developer guide: View

    0 讨论(0)
提交回复
热议问题