Mode 1: Do not inherit any components and invoke instantiation directly inside the code.
public class ProgressDialog {
Private Dialog Dialog;
Public ProgressDialog (@NonNull context context) {//constructor must have
Dialog = New dialog (context);
Builddialog (context);
}
...... Other logical methods
}
Mode 2: Individual controls, inherited and view, can be called on XML (cannot be previewed because it needs to be OnDraw drawn at runtime)
public class Progresspieview extends View {
Public Progresspieview (Context context) {
This (context, NULL);
}
Public Progresspieview (context context, AttributeSet attrs) {//must have a AttributeSet constructor added to write an error on the XML layout
This (context, attrs, 0);
}
Public Progresspieview (context context, AttributeSet attrs, int defstyleattr) {
Super (context, attrs, defstyleattr);
Init (context, attrs);//exposure to custom methods ...
}
@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//Control control gesture conflict and height
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
......
}
@Override
protected void OnDraw (canvas canvas) {//Draw, draw with canvas ... , there's no such method in Linerlayout,relatitylayout,framelayout.
Super.ondraw (canvas);
......
}
@Override
public void layout (int l, int t, int r, int b) {//Control layout position
......
}
@Override
protected void Onattachedtowindow () {///layout is executed when attached to the main layout view; no more manual resource recycling
Super.onattachedtowindow ();
}
@Override
protected void Ondetachedfromwindow () {///layout is removed from the main layout; no more manual resource recycling
Super.ondetachedfromwindow ();
}
...... Other logical methods
}
Mode 3: A composite control with multiple controls, inherited with Linerlayout or relatitylayout or framelayout; can be written directly on XML (preview)
;;; layout layouts itself is inheritance and ViewGroup ViewGroup viewgroup!
public class Girdmenuview extends Framelayout {
Private Recyclerview Mrecyclerview;
Private list<categoriesmodel> datas = new arraylist<> ();
constructor is executed when loading, XML layout is executed.
Public Girdmenuview (@NonNull context context) {
This (context, NULL);
}
Public Girdmenuview (@NonNull context context, @Nullable AttributeSet attrs) {////must have a AttributeSet constructor added, To write an error on the XML layout.
This (context, attrs, 0);
}
Public Girdmenuview (@NonNull context context, @Nullable AttributeSet attrs, @AttrRes int defstyleattr) {
Super (context, attrs, defstyleattr);
Initviews (context, attrs);//exposure to custom methods ...
}
@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//Control control gesture conflict and height, not must set ~
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
......
}
@Override
protected void OnLayout (Boolean changed, int left, int top, int. right, int bottom) {//Load layout, control position
......
}
@Override
protected void Onattachedtowindow () {///layout is executed when attached to the main layout view; no more manual resource recycling
Super.onattachedtowindow ();
if (mprogressdrawable! = null) {
Mprogressdrawable.start ();
}
}
@Override
protected void Ondetachedfromwindow () {///layout is removed from the main layout; no more manual resource recycling
Super.ondetachedfromwindow ();
if (null! = mprogressdrawable) {
Mprogressdrawable.stop ();
}
}
}
Mode 4: Inherit existing controls directly, extend to existing controls, like inheritance and view (but preview); All controls that come with the system are inherited and view, which implements the OnDraw () method
public class Customviewpager extends Viewpager {
private static final String TAG = CustomViewPager.class.getSimpleName ();
private float Mtouchx;
private float Mtouchy;
Public Customviewpager (Context context) {
Super (context);
}
Public Customviewpager (context context, AttributeSet Attrs) {
Super (context, attrs);
}
@Override
public boolean onintercepttouchevent (Motionevent event) {
Return Super.onintercepttouchevent (event);
}
@Override
public boolean ontouchevent (Motionevent event) {
Return Super.ontouchevent (event);
}
}
Attention:
1 to throw and expose results, you can combine the callback function with the
2 You can refer to the system's own source of control, to see the relevant methods, it is easier to see how the custom control.
Overview of several ways to customize Android controls