Three Ways for Android to Obtain view Height

  • 2021-07-18 09:02:54
  • OfStack

This article shares the method of obtaining view height from Android for your reference. The specific contents are as follows

Differences between getMeasuredHeight () and getHeight
In fact, when the screen can wrap the content, their values are equal.
Only when view goes beyond the screen can we see the difference between them:
getMeasuredHeight () is the size of the actual View, regardless of the screen,
The size of getHeight is now the size of the screen.
When beyond the screen, getMeasuredHeight () is equal to getHeight () plus the size not displayed outside the screen
Specific method
We know that in oncreate View. getWidth and View. getHeight cannot get the height and width of one view because the View component layout is completed after the onResume callback.
Here are three ways
getViewTreeObserver
Use getViewTreeObserver (). addOnGlobalLayoutListener () for width or height.
OnGlobalLayoutListener is an internal class of ViewTreeObserver. When the layout of a view tree changes, it can be monitored by ViewTreeObserver. This is an observer who registers to monitor the view tree (observer) and is notified when the global events of the view tree change. ViewTreeObserver cannot be instantiated directly, but is obtained through getViewTreeObserver ().
In addition to OnGlobalLayoutListener, ViewTreeObserver has the following internal classes:
interfaceViewTreeObserver.OnGlobalFocusChangeListener
When the focus state in 1 view tree changes, the interface class of the callback function to be called
interfaceViewTreeObserver.OnGlobalLayoutListener
The interface class of the callback function to be called when the global layout changes in 1 view tree or the visual state of a view in the view tree changes
interfaceViewTreeObserver.OnPreDrawListener
The interface class of the callback function to be called when a view tree is about to be drawn
interfaceViewTreeObserver.OnScrollChangedListener
The interface class of the callback function to be called when 1 component in 1 view tree scrolls
interfaceViewTreeObserver.OnTouchModeChangeListener
When the touch mode of a view tree changes, the interface class of the callback function to be called
Among them, we can use OnGlobalLayoutListener to get the true height of 1 view.


private int mHeaderViewHeight; 
private View mHeaderView; 
 
..... 
 
mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener( 
 new OnGlobalLayoutListener() { 
  @Override 
  public void onGlobalLayout() { 
                                                         
   mHeaderViewHeight = mHeaderView.getHeight(); 
   mHeaderView.getViewTreeObserver() 
     .removeGlobalOnLayoutListener(this); 
  } 
}); 

However, it should be noted that OnGlobalLayoutListener may be triggered many times, so OnGlobalLayoutListener should be logged off after getting height.
View post event
It can also be obtained in the post method of VIew


public class TestHeight extends Activity { 
 TextView tv; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_activity_b); 
   tv = (TextView) findViewById(R.id.textView); 
  tv.post(new Runnable() { 
   @Override 
   public void run() { 
    int height= tv.getHeight(); 
   } 
  }); 
 } 
 
 
} 

Direct measurement calculation


int intw=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); 
int inth=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); 
textView.measure(intw, inth); 
int intwidth = textView.getMeasuredWidth(); 
int intheight = textView.getMeasuredHeight(); 

Related articles: