Android development process, the official control of Android sometimes comes from often not meet our needs. This time, I have to define myself. Let's take a look at their definition 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 ();//Draw Again}}
In XML we want to write our own defined controls such as the following:
<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>
It's over. A very easy to define your own view. Program on the road together, refueling!
Yes. Progress a little! It will eventually become God's!
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Android their definition view