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: