Android their definition view

Source: Internet
Author: User

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

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.