Difference between getmeasuredheight () and getheight

Source: Internet
Author: User

Address: http://blog.csdn.net/xujainxing/article/details/8985063

Many articles talk about the difference between getmeasuredheight () and getheight, but they are all about "romantic nonsense ".I have to admit that I have to find the answer from the official document.

However, when the screen can actually wrap content, their values are equal. Only when the view exceeds the screen can we see their difference: getmeasuredheight () is the actual view size, the size of getheight is the size of the screen. When the screen is exceeded,Getmeasuredheight ()EqualGetheight () plus the size not displayed outside the screen

Let's take a look at this example:

 

[HTML]View plaincopy
  1. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  2. Xmlns: Tools = "http://schemas.android.com/tools"
  3. Android: layout_width = "match_parent"
  4. Android: layout_height = "match_parent"
  5. Android: Orientation = "vertical"
  6. >
  7. <Textview
  8. Android: Id = "@ + ID/textview1"
  9. Android: layout_width = "wrap_content"
  10. Android: layout_height = "fill_parent"
  11. Android: text = "textview"/>
  12. </Linearlayout>



System. Out. println ("getmeasuredheight () ------------->" + textview. getmeasuredheight ());
System. Out. println ("getheight () --------------->" + textview. getheight ());

 

The result is that the two are the same size, both of which are the screen size. Therefore, we simply think thatGetheight () is the actual size of the view and is also related to the layout.

The method used to obtain the control size in the middle of the activity:

 

[Java]View plaincopy
  1. Viewtreeobserver VTO = textview. getviewtreeobserver ();
  2. VTO. addonpredrawlistener (New viewtreeobserver. onpredrawlistener (){
  3. Public Boolean onpredraw (){
  4. System. Out. println ("getmeasuredheight () textview ------------->" + textview. getmeasuredheight ());
  5. System. Out. println ("getheight () textview ------------->" + textview. getheight ());
  6. Return true;
  7. }
  8. });

How to dynamically change the layout size:

Layoutparams Lp = Control ID. getlayoutparams ();
LP. Height = 480;

Control ID. setlayoutparams (LP );

After ID. setlayoutparams (LP) is used, you need to add ID. requestlayout ();

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.