Android Custom View

Source: Internet
Author: User

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!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.