The Setmeasureddimension method in Android view

Source: Internet
Author: User

To inherit view and implement the component you want, you need to use the Setmeasureddimension method, which determines the size of the current view, see Code:

View Code: Java code    package cc.mdev.test;      import  android.content.context;   import android.graphics.canvas;   import  android.graphics.color;   import android.text.textpaint;   import  android.util.attributeset;   import android.util.log;   import android.view.View;       public class myscrollview extends view {       Public myscrollview (Context context, attributeset attrs)  {   super ( Context, attrs);  }      Public myscrollview (context context)   {   Super (context);  }   @Override    Protected void ondraw ( Canvas canvas)  {   textpaint paint = new textpaint ();   Paint.setantialias (true);   Canvas.drawcolor(Color.gray);   for  (int i = 10; i < 500; i++)  {    canvas.drawtext ("This is the scroll text.",  10, i, paint);    i = i+15;  }  }      @Override    Protected void onmeasure (Int widthmeasurespec, int heightmeasurespec)  {    string tag= "Onmeasure";   log.e (tag,  "scroll view on measure ...");    setmeasureddimension (200, 800);  }      @Override    Protected void onscrollchanged (Int l, int t, int oldl, int oldt)  {   string tag =  "onscrollchanged";   log.e (tag,  "Scroll ...");    super.onscrollchanged (L, t, oldl, oldt);  }  }    




Layout files: XML code <?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/" Apk/res/android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_ Parent "android:background=" #fff > <button android:text= "Button01" android:id= "@+id/button01" Android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" ></Button> <scrollview android:layout_width= "Fill_parent" android:layout_height= "fill_parent" > <cc.mdev.test.myscrollview android:layout_width= "WRAP_" Content "android:layout_height=" wrap_content "/> </ScrollView> </LinearLayout>



The effect is that the size of the custom view is
200, 800, and put into the ScrollView, ScrollView will function, if you do not use the Setmeasureddimension method, then
ScrollView will not work.

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.