How to retrieve the dimensions of a view?

前端 未结 16 1537
小蘑菇
小蘑菇 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:25

    Use the View's post method like this

    post(new Runnable() {   
        @Override
        public void run() {
            Log.d(TAG, "width " + MyView.this.getMeasuredWidth());
            }
        });
    
    0 讨论(0)
  • 2020-11-22 01:25

    Even though the proposed solution works, it might not be the best solution for every case because based on the documentation for ViewTreeObserver.OnGlobalLayoutListener

    Interface definition for a callback to be invoked when the global layout state or the visibility of views within the view tree changes.

    which means it gets called many times and not always the view is measured (it has its height and width determined)

    An alternative is to use ViewTreeObserver.OnPreDrawListener which gets called only when the view is ready to be drawn and has all of its measurements.

    final TextView tv = (TextView)findViewById(R.id.image_test);
    ViewTreeObserver vto = tv.getViewTreeObserver();
    vto.addOnPreDrawListener(new OnPreDrawListener() {
    
        @Override
        public void onPreDraw() {
            tv.getViewTreeObserver().removeOnPreDrawListener(this);
            // Your view will have valid height and width at this point
            tv.getHeight();
            tv.getWidth();
        }
    
    });
    
    0 讨论(0)
  • 2020-11-22 01:26

    You should rather look at View lifecycle: http://developer.android.com/reference/android/view/View.html Generally you should not know width and height for sure until your activity comes to onResume state.

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

    Simple Response: This worked for me with no Problem. It seems the key is to ensure that the View has focus before you getHeight etc. Do this by using the hasFocus() method, then using getHeight() method in that order. Just 3 lines of code required.

    ImageButton myImageButton1 =(ImageButton)findViewById(R.id.imageButton1); myImageButton1.hasFocus();

    int myButtonHeight = myImageButton1.getHeight();

    Log.d("Button Height: ", ""+myButtonHeight );//Not required

    Hope it helps.

    0 讨论(0)
  • I tried to use onGlobalLayout() to do some custom formatting of a TextView, but as @George Bailey noticed, onGlobalLayout() is indeed called twice: once on the initial layout path, and second time after modifying the text.

    View.onSizeChanged() works better for me because if I modify the text there, the method is called only once (during the layout pass). This required sub-classing of TextView, but on API Level 11+ View. addOnLayoutChangeListener() can be used to avoid sub-classing.

    One more thing, in order to get correct width of the view in View.onSizeChanged(), the layout_width should be set to match_parent, not wrap_content.

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

    I guess this is what you need to look at: use onSizeChanged() of your view. Here is an EXTENDED code snippet on how to use onSizeChanged() to get your layout's or view's height and width dynamically http://syedrakibalhasan.blogspot.com/2011/02/how-to-get-width-and-height-dimensions.html

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