Custom General Sub-three cases
1. Custom Layouts
2. Custom Controls
3. Direct Inheritance View
Here's the EoE example, implementing a custom control
1. Custom Attributes
Res/values/attrs.xml Custom Properties
<?XML version= "1.0" encoding= "Utf-8"?><Resources> <declare-styleablename= "Progress"> <attrname= "text"format= "string" /> <attrname= "TextSize"format= "Dimension" /> </declare-styleable></Resources>
2. Customizing the control to implement a construction method with AttributeSet
PackageCom.test.uidemo;ImportAndroid.content.Context;ImportAndroid.content.res.TypedArray;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;ImportAndroid.widget.TextView;ImportCOM.TEST.UIDEMO.R;/*** Refer to EOE Books * Custom controls * Implements wrapping text with a border.*/ Public classMytextviewextendsTextView { PublicMytextview (Context context) {Super(context); } PublicMytextview (Context context, AttributeSet attrs) {Super(context, attrs); TypedArray attr=context.obtainstyledattributes (Attrs, r.styleable.progress); SetText (attr.getstring (R.styleable.progress_text)); Settextsize (Attr.getdimension (R.styleable.progress_textsize,16)); LOG.I ("Text", Attr.getstring (R.styleable.progress_text)); } protected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); Paint Paint=NewPaint (); Paint.setcolor (Android.graphics.Color.RED); Canvas.drawline (0,0, This. GetWidth () -1,0, paint); Canvas.drawline (0,0,0, This. GetHeight ()-1, paint); Canvas.drawline ( This. GetWidth () -1,0, This. GetWidth ()-1, This. GetHeight ()-1, paint); Canvas.drawline (0, This. GetHeight ()-1, This. GetWidth ()-1, This. GetHeight ()-1, paint); }}
3. Using a custom control
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"xmlns:progress= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.test.uidemo.MainActivity" > <Com.test.uidemo.MyTextView android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Progress:text= "Custom"progress:textsize= "30DP"android:layout_centervertical= "true"Android:layout_alignparentright= "true"Android:layout_alignparentend= "true" > </com.test.uidemo.MyTextView></RelativeLayout>
View Code
Android Custom Controls