Android custom View

Source: Internet
Author: User

During project development, some widgets provided by the system may not meet our needs. In this case, you need to customize the View.

By viewing common widgets such as Button, TextView, and EditText in the system, they all inherit from the View. Therefore, we naturally need to inherit the View when inheriting the custom View.
1. First create a class LView inherited from View

Copy codeThe Code is as follows: public class LView extends View {
Private Paint paint;

Public LView (Context context ){
Super (context );
}

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

@ Override
Protected void onDraw (Canvas canvas ){
Super. onDraw (canvas );
Paint = new Paint (); // a new paint brush
Paint. setColor (Color. RED); // you can specify the paint Color.
Paint. setStyle (Style. FILL); // you can specify the paint pad.
Canvas. drawCircle (50, 50, 40, paint); // use a paint brush to add a circle on the canvas. You can not only add a circle, but also a rectangle!
Paint. setColor (Color. YELLOW); // you can specify the paint Color.
Canvas. drawText ("LView", 50, 50, paint); // use a paint brush to add text to the canvas. The two parameters in the middle correspond to coordinates.
}
}

2. Configure in the layout File
Copy codeThe Code is 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 and you can see the following screen:

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.