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: