In the process of Android development, Android's own controls sometimes don't meet our needs. This time I need to customize. Let's take a look at the custom view:
Package Com.example.myview;import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.paint.style;import Android.util.attributeset;import Android.view.view;import Android.view.view.onclicklistener;public class MyView Extends View implements Onclicklistener{private int a=0;private Paint paint;public MyView (context context) {Super ( Context)//TODO auto-generated constructor stub} public MyView (context context, AttributeSet Attrs) {Super (context, ATT RS); } @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas);p aint=new paint ();//define a brush Paint.setcolor ( color.red);//Set the brush color Paint.setstyle (Style.fill);//Set the Brush fill canvas.drawcircle (. Draw a circle Paint.setcolor (Color.Blue),//Set the color Paint.settextsize (20),//Set the font size canvas.drawtext ("small source" +a, the three, the three, paint); Setonclicklistener (this),//For view plus listener @Overridepublic void OnClick (view arg0) {//TODO auto-generated method stuba++; Invalidate ();//Redraw}}
In XML we want to write our custom controls as follows:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" 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=". Mainactivity "><com.example.myview.myview android:layout_width=" wrap_content " android:layout_ height= "Wrap_content"/></relativelayout>
This completes a very simple custom view. The program on the road together, refueling!
All right, a little bit in progress! Will eventually become the great God!