The last time the custom control refreshes any part of the screen. If a textview is placed under the custom control in XML, this textview cannot be displayed, the previous custom controls are
Why? Onmeasure ()
Override the onmeasure Method Based on the custom refresh control
Modify according to the custom component in the previous article
Comment in the code
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"> <Xue. test. cusview3android: Id = "@ + ID/cusview3" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </Xue. test. cusview3> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "I have finally appeared"/> </linearlayout>
The textview cannot be displayed. to display the textview, measure the size of the control.
Public class cusview3 extends view {private int color = 0; private string text = "click to refresh"; private paint mpaint; private int mascent; Public cusview3 (context, attributeset attrs) {super (context, attrs); mpaint = new paint (); mpaint. setstyle (style. fill); mpaint. settextsize (35366f); setpadding (20, 60, 0, 0); // set padding} @ overrideprotected void ondraw (canvas) {super. ondraw (canvas); If (color> 2) {Color = 0;} switch (color) {Case 0: mpaint. setcolor (color. green); break; Case 1: mpaint. setcolor (color. red); break; Case 2: mpaint. setcolor (color. blue); break; default: break;} canvas. drawtext (text, getpaddingleft (), getpaddingtop (), mpaint);} public void changecolor () {color ++ ;} /*** execute first than ondraw ** A measurespec encapsulates the layout requirements that the parent layout passes to the Child layout. Each measurespec represents a set of width and height requirements. * A measurespec consists of size and mode * It has three modes: unspecified (unspecified). The parent element forces any constraint from the element, and the child element can get any desired size; * exactly (full): the parent element determines the exact size of the Self-element. The child element is limited to the given boundary and its size is ignored. * at_most (maximum ), the child element must not exceed the value of the specified size. ** Three common functions: * 1. static int getmode (INT measurespec): extract the mode based on the provided measured value (Format) (one of the preceding three modes) * 2. static int getsize (INT measurespec): extract the size value based on the provided measured value (Format) (this size is what we usually call the size) * 3. static int makemeasurespec (INT size, int mode): Creates a measurement value (Format) */@ overrideprotected void onmeasure (INT widthmeasurespec, int heightmeasurespec) based on the provided size and mode) {setmeasureddimension (measurewidth (widthmeasurespec), measureheight (heightmeasurespec);} private int measurewidth (INT measurespec) {int result = 0; int specmode = measurespec. getmode (measurespec); int specsize = measurespec. getsize (measurespec); If (specmode = measurespec. exactly) {// we were told how big to beresult = specsize;} else {// measure the textresult = (INT) mpaint. measuretext (text) + getpaddingleft () + getpaddingright (); If (specmode = measurespec. at_most) {// respect at_most value if that was what is called for by // measurespecresult = math. min (result, specsize); // 60,480} return result;} private int measureheight (INT measurespec) {int result = 0; int specmode = measurespec. getmode (measurespec); int specsize = measurespec. getsize (measurespec); mascent = (INT) mpaint. ascent (); If (specmode = measurespec. exactly) {// we were told how big to beresult = specsize;} else {// measure the text (beware: ascent is a negative number) Result = (INT) (-mascent + mpaint. descent () + getpaddingtop () + getpaddingbottom (); If (specmode = measurespec. at_most) {// respect at_most value if that was what is called for by // measurespecresult = math. min (result, specsize) ;}} return result ;}}
Code http://download.csdn.net/detail/ethan_xue/4178423