Introduction to the use of Android custom view _android

Source: Internet
Author: User

In the development of the project, it is possible that some of the widgets brought by the system do not meet our needs, then we need to customize the view.

By looking at common widgets like Button,textview,edittext in the system, they all inherit from view, so we naturally need to inherit view when inheriting custom view.
1, first create a new class LView inherited from view

Copy Code code as follows:

public class LView extends View {
Private Paint Paint;

Public LView {
Super (context);
}

Public LView (context context, AttributeSet Attrs) {
Super (context, attrs);
}

@Override
protected void OnDraw (Canvas Canvas) {
Super.ondraw (canvas);
Paint = new Paint ()//New Brush
Paint.setcolor (color.red);//Set Brush color
Paint.setstyle (Style.fill);/Set Brush fill
Canvas.drawcircle (M, m, paint)//////////////////////////////////////////
Paint.setcolor (Color.yellow);//Set Brush color
Canvas.drawtext ("LView", m, paint)///////////////////////
}
}


2, in the layout file to configure
Copy Code code as follows:

<button
Android:id= "@+id/btn"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>

<com.androidstudy.lview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>


Run the program, you can see the following screen:

Related Article

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.