1. Define the properties of your view
2. Get our own defined properties in the view's construction method
3. Rewrite onmesure
4. Rewrite OnDraw
3 This step is not necessary, of course, most of the cases still need to be rewritten.
1, you define the properties of the view, first set up a attrs.xml under res/values/, in which we define our properties and declare our entire style.
<?xml version= "1.0" encoding= "Utf-8"?><resources> <attr name= "txtname" format= "string"/> <attr name= "txtcolor" format= "color"/> <attr name= "txtsize" format= "Dimension"/> < Declare-styleable name= "TitleStyle" > <attr name= "txtname"/> <attr name= "Txtcolor"/> <attr name= "Txtsize"/> </declare-styleable></resources>
Defines the font, font color, font size 3 properties, format is the value type of the property:
A common ownership: String,color,demension,integer,enum,reference,float,boolean,fraction,flag;
When you write the tool will remind you to use which, do not know also can Google search under
Next, define the view yourself.
public class Customtitleview extends view{ private String txtname; private int txtcolor,txtsize; Private Paint Mpaint; Private Rect mbounds; Public Customtitleview (Context context) {This (context, NULL); } Public Customtitleview (context context, AttributeSet attrs) {This (context, attrs, 0); } Public Customtitleview (context context, AttributeSet attrs, int defstyleattr) { //detailed operation }}
When you define your own view, it is time to call our own definition of view.
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " xmlns:title=" Http://schemas.android.com/apk/res/com.example.androidDemo "" " Android:o rientation= "vertical" android:layout_width= "match_parent" android:layout_height= "match_parent" > < Com.example.androidDemo.View.CustomTitleView android:layout_width= "wrap_content" android:layout_height = "Wrap_content" android:layout_centerhorizontal= "true" android:layout_centervertical= "true" android:padding= "5DP" title:txtname= "Hello" title:txtcolor= "#ffffff" title:txtsize= "16SP"/>< /relativelayout>
Note This line in the code, define the namespace yourself, Com.example.androidDemo is the project package path
Xmlns:title= "Http://schemas.android.com/apk/res/com.example.androidDemo"
To define a namespace with yourself:
Title:txtname= "Hello" title:txtcolor= "#ffffff" title:txtsize= "16SP"
In the construction method of the view, get our own defined style
public class Customtitleview extends view{private String txtname; private int txtcolor,txtsize; Private Paint Mpaint; Private Rect mbounds; Public Customtitleview (Context context) {This (context, NULL); } public Customtitleview (context context, AttributeSet Attrs) {This (context, attrs, 0); } public Customtitleview (context context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, Defstyle ATTR); TypedArray TypedArray = Context.gettheme (). Obtainstyledattributes (attrs,r.styleable.titlestyle,defstyleattr,0); int n = typedarray.getindexcount (); for (int i = 0; i < n; i++) {int attr = Typedarray.getindex (i); Switch (attr) {Case 0:txtname = typedarray.getstring (attr); Break Case 1:txtcolor = Typedarray.getcolor (attr, Color.Black); Break Case 2: Txtsize = Typedarray.getdimensionpixelsize (attr, (int) typedvalue.applydimension (typedvalue. COMPLEX_UNIT_SP, Getresources () getdisplaymetrics ()); Break }} typedarray.recycle (); /** * Get the width and height of the drawn text */mpaint = new paint (); Mpaint.settextsize (txtsize); Mpaint.setcolor (Mtitletextcolor); Mbounds = new Rect (); Mpaint.gettextbounds (txtname, 0, Txtname.length (), mbounds); } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {int widthmode = Measu Respec.getmode (WIDTHMEASURESPEC); int widthsize = measurespec.getsize (Widthmeasurespec); int heightmode = Measurespec.getmode (Heightmeasurespec); int heightsize = measurespec.getsize (Heightmeasurespec); int width; int height; if (Widthmode = = measurespec.exactly) {width =Widthsize; } else {mpaint.settextsize (txtsize); Mpaint.gettextbounds (txtname, 0, Txtname.length (), mbounds); float textWidth = Mbounds.width (); int desired = (int) (Getpaddingleft () + TextWidth + getpaddingright ()); width = desired; } if (Heightmode = = measurespec.exactly) {height = heightsize; } else {mpaint.settextsize (txtsize); Mpaint.gettextbounds (txtname, 0, Txtname.length (), mbounds); float textHeight = Mbounds.height (); int desired = (int) (Getpaddingtop () + TextHeight + getpaddingbottom ()); height = desired; } setmeasureddimension (width, height); } @Override protected void OnDraw (canvas canvas) {mpaint.setcolor (color.yellow); Canvas.drawrect (0, 0, getmeasuredwidth (), Getmeasuredheight (), MpaiNT); Mpaint.setcolor (Txtcolor); Canvas.drawtext (Txtname, getwidth ()/2-mbounds.width ()/2, GetHeight ()/2 + mbounds.height ()/2, mpaint); }}
Of
measurespec.exactly infer the width and height of your descendants is not an exact assignment.
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Assuming it's wrap_content,
Mpaint.settextsize (txtsize); Mpaint.gettextbounds (txtname, 0, Txtname.length (), mbounds); float textWidth = Mbounds.width (); int desired = (int) (Getpaddingleft () + TextWidth + getpaddingright ()); width = desired;
The assumption is that the exact width and height values of 200DP
if (Widthmode = = measurespec.exactly) { width = widthsize;
, is it very useful?
"Android own definition control" define the View property yourself