Android programming Get control Width and Height method summary analysis _android

Source: Internet
Author: User

This paper summarizes and analyzes the method of acquiring control width and height in Android programming. Share to everyone for your reference, specific as follows:

We all know that the height of the control in OnCreate () is 0, why? Let's take a look at the example:

First we write our own control, which is very simple:

public class Myimageview extends ImageView {public
  Myimageview (context context, AttributeSet Attrs) {
    super ( context, attrs);
  }
  Public Myimageview {
    super (context);
  }
  @Override
  protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
    super.onmeasure ( Widthmeasurespec, Heightmeasurespec);
    System.out.println ("Onmeasure I was called" +system.currenttimemillis ());
  }
  @Override
  protected void OnDraw (Canvas Canvas) {
    super.ondraw (Canvas);
    System.out.println ("OnDraw I was called" +system.currenttimemillis ());
  }


Layout file:

<com.test.myimageview
  android:id= "@+id/imageview"
  android:layout_width= "Wrap_content"
  android: layout_height= "Wrap_content"
  android:src= "@drawable/test"/>

OnCreate of the Tested activity ():

@Override public
void OnCreate (Bundle savedinstancestate) {
  super.oncreate (savedinstancestate);
  Setcontentview (r.layout.main);
  System.out.println ("Execution completed ...") +system.currenttimemillis ());
}

Now let's take a look at the results:

Note The OnCreate method is finished, and the controls we define are measured (measure), so we get the height or width of the control through View.getheight () in the OnCreate method is definitely 0, Because it has not been measured, that is to say, he does not know how high he is, and you are at this time to obtain its size, certainly not.

Now we have to solve the problem, we found the following methods on the Internet:

------------------------------------------------method One int w = View.MeasureSpec.makeMeasureSpec (0,
View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified);
Imageview.measure (W, h);
int height =imageview.getmeasuredheight ();
int width =imageview.getmeasuredwidth ();
Textview.append ("\ n" +height+ "," +width);
-----------------------------------------------Method Two Viewtreeobserver vto = Imageview.getviewtreeobserver (); Vto.addonpredrawlistener (New Viewtreeobserver.onpredrawlistener () {public Boolean onpredraw () {int height = Imagev
    Iew.getmeasuredheight ();
    int width = imageview.getmeasuredwidth ();
    Textview.append ("\ n" +height+ "," +width);
  return true;
}
});
-----------------------------------------------Method Three Viewtreeobserver Vto2 = Imageview.getviewtreeobserver (); Vto2.addongloballayoutlistener (New Ongloballayoutlistener () {@Override public void Ongloballayout () {IMAGEVIEW.G Etviewtreeobserver (). removeglobalonlayoutlistEner (this);
  Textview.append ("\ n" +imageview.getheight () + "," +imageview.getwidth ());

 }
});

These three ways to find out are now forgotten.

Now what is to be discussed when we need to use which method?

Now change the test activity to the following:

@Override public
 void OnCreate (Bundle savedinstancestate) {
   super.oncreate (savedinstancestate);
   Setcontentview (r.layout.main);
   Final ImageView ImageView = (imageview) Findviewbyid (R.id.imageview);
   ------------------------------------------------method one
   int w = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED);
   int h = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified);
   Imageview.measure (W, h);
   int height =imageview.getmeasuredheight ();
   int width =imageview.getmeasuredwidth ();
   Textview.append ("\ n" +height+ "," +width);
   System.out.println ("Execution completed ...") +system.currenttimemillis ());
 }

Then look at the following ways to output the results:

Change the test activity into the following:

@Override public
void OnCreate (Bundle savedinstancestate) {
  super.oncreate (savedinstancestate);
  Setcontentview (r.layout.main);
  Final ImageView ImageView = (imageview) Findviewbyid (R.id.imageview);
-----------------------------------------------method Two
  viewtreeobserver vto = Imageview.getviewtreeobserver ();
  Vto.addonpredrawlistener (New Viewtreeobserver.onpredrawlistener () {public
    Boolean Onpredraw () {
      int height = Imageview.getmeasuredheight ();
      int width = imageview.getmeasuredwidth ();
      Textview.append ("\ n" +height+ "," +width);
      return true;}}
  );


The results are as follows:

Method Three will no longer test the same method two!!!

So what is the difference between method and method three in implementation?

We add a textview to the layout file to record the height of the control.

<scrollview
  android:layout_width= "wrap_content"
  android:layout_height= "wrap_content" >
  < TextView
    android:id= "@+id/text"
    android:layout_width= wrap_content "android:layout_height=" Wrap_
    Content "/>
</ScrollView>

First Test Method Two:

@Override public
void OnCreate (Bundle savedinstancestate) {
  super.oncreate (savedinstancestate);
  Setcontentview (r.layout.main);
  Final ImageView ImageView = (imageview) Findviewbyid (R.id.imageview);
-----------------------------------------------method Two
  viewtreeobserver vto = Imageview.getviewtreeobserver ();
  Vto.addonpredrawlistener (New Viewtreeobserver.onpredrawlistener () {public
    Boolean Onpredraw () {
      int Height = imageview.getmeasuredheight ();
      int width = imageview.getmeasuredwidth ();
      Textview.append ("\ n" +height+ "," +width);
      return true;}}
  );


The results are as follows:

We're going to test the method three

@Override public
void OnCreate (Bundle savedinstancestate) {
  super.oncreate (savedinstancestate);
  Setcontentview (r.layout.main);
  Final ImageView ImageView = (imageview) Findviewbyid (R.id.imageview);
  -----------------------------------------------method Three
  Viewtreeobserver Vto2 = Imageview.getviewtreeobserver ();
  Vto2.addongloballayoutlistener (New Ongloballayoutlistener () {
    @Override public
    void Ongloballayout () {
      imageview.getviewtreeobserver (). Removeglobalonlayoutlistener (this);
      Textview.append ("\ n" +imageview.getheight () + "," +imageview.getwidth ());}}
  );


The output results are as follows:

I think the difference between the method two and the method is needless to say.

Summary: So what is the way to get the height of the control?

Method One: One more calculation than the other two methods, that is, multiple calls to the Onmeasure () method, although it looks simple, but if you want the target control to calculate time-consuming (such as ListView, etc.), it is not recommended.

Method Two, its callback method will be called many times, and it will be invoked when sliding textview, so it is not recommended.

Method three, more appropriate.

Of course, the actual application needs to be based on the actual situation.

I hope this article will help you with the Android program.

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.