Android gets three ways to view Heights _android

Source: Internet
Author: User

This article for everyone to share the Android Access view height of the method for your reference, the specific content as follows

getmeasuredheight ()AndgetheightThe difference
In fact, when the screen can wrap the content, their values are equal,
Only when the view is out of the screen can you see the difference:
Getmeasuredheight () is the size of the actual view, regardless of the screen,
The size of the getheight is now the size of the screen.
When the screen is exceeded, the getmeasuredheight () equals getheight () plus the size not displayed outside the screen
Specific methods
We know that View.getwidth and view.getheight in OnCreate cannot get the height and width of a view because the view component layout is completed after the Onresume callback.
here are three ways.
Getviewtreeobserver
Usegetviewtreeobserver (). Addongloballayoutlistener ()To get the width or height.
Ongloballayoutlistener is the inner class of the viewtreeobserver, which can be monitored by viewtreeobserver when the layout of a view tree changes, which is an observer (Observer) registering the Listening view tree, Be notified when global events in the View tree change. Viewtreeobserver cannot be directly instantiated, but is obtained by Getviewtreeobserver ().
In addition to Ongloballayoutlistener, Viewtreeobserver also has the following internal classes:
Interfaceviewtreeobserver.onglobalfocuschangelistener
The interface class of the callback function to be invoked when the focus state in a view tree changes
Interfaceviewtreeobserver.ongloballayoutlistener
The interface class of the callback function to invoke when the global layout changes in one view tree or when the visual state of a view in the view tree changes.
Interfaceviewtreeobserver.onpredrawlistener
The interface class of the callback function to invoke when a view tree will be drawn
Interfaceviewtreeobserver.onscrollchangedlistener
When some components in a view tree scroll, the interface class of the callback function that you want to call
Interfaceviewtreeobserver.ontouchmodechangelistener
The interface class of the callback function to invoke when a view tree's touch mode changes
Among them, we canusing Ongloballayoutlistener to get the true height of a view

private int mheaderviewheight; 
Private View Mheaderview; 
 
..... 
 
Mheaderview.getviewtreeobserver (). Addongloballayoutlistener ( 
 new Ongloballayoutlistener () { 
  @Override Public 
  void Ongloballayout () { 
                                                         
   mheaderviewheight = Mheaderview.getheight (); 
   Mheaderview.getviewtreeobserver () 
     . Removeglobalonlayoutlistener (this); 
  } 
); 

Note, however, that Ongloballayoutlistener may be triggered multiple times, so after you get a height, you should log off the Ongloballayoutlistener.
The View post event gets
You can also get it 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 (); 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.