android---自定义控件 几种方式总结

方式1:不继承任何组件 , 直接在代码里面调用实例化。
public class ProgressDialog {

private Dialog dialog;

public ProgressDialog(@NonNull Context context) { //构造函数必须有
dialog = new Dialog(context);
buildDialog(context);
}

……其他逻辑方法

}

 


方式2:单独控件,继承与View ,可以在xml上调用(无法预览,因为需要在运行时候才onDraw绘制)
public class ProgressPieView extends View {



public ProgressPieView(Context context) {
this(context, null);
}

public ProgressPieView(Context context, AttributeSet attrs) {//必须添加有AttributeSet 的构造函数,才能在xml布局上编写否则报错
this(context, attrs, 0);
}

public ProgressPieView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

init(context, attrs);//暴露出来的自定义方法。。。
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//控制控件手势冲突 和 高度
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
……
}


@Override
protected void onDraw(Canvas canvas) { // 绘制,用画布绘制。。。,Linerlayout,RelatityLayout,FrameLayout里面没有这个方法
super.onDraw(canvas);

……
}


@Override
public void layout(int l, int t, int r, int b) {//控制布局位置
……
}

 

@Override
protected void onAttachedToWindow() {//布局 附加到主 布局视图中时会执行;不用再手动资源回收了
super.onAttachedToWindow();

}

@Override
protected void onDetachedFromWindow() {//布局 从主布局移除会执行;不用再手动资源回收了
super.onDetachedFromWindow();

}


……其他逻辑方法
}

 

方式3:组合控件,里面有多个控件的,继承与Linerlayout或者RelatityLayout或FrameLayout;可在xml上直接编写(可预览)
;;;layout布局本身就是继承与 ViewGroup ViewGroup ViewGroup!
public class GirdMenuView extends FrameLayout {

private RecyclerView mRecyclerView;
private List<CategoriesModel> datas = new ArrayList<>();

//执行加载,xml布局的时候,就会执行构造函数
public GirdMenuView(@NonNull Context context) {
this(context, null);
}

public GirdMenuView(@NonNull Context context, @Nullable AttributeSet attrs) {////必须添加有AttributeSet 的构造函数,才能在xml布局上编写否则报错
this(context, attrs, 0);
}

public GirdMenuView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);

initViews(context, attrs);//暴露出来的自定义方法。。。
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//控制控件手势冲突 和 高度,不是必须设置~
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
……
}


@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {//加载布局时候,控制位置
……
}


@Override
protected void onAttachedToWindow() {//布局 附加到主 布局视图中时会执行;不用再手动资源回收了
super.onAttachedToWindow();
if (mProgressDrawable != null) {

mProgressDrawable.start();
}
}

@Override
protected void onDetachedFromWindow() {//布局 从主布局移除会执行;不用再手动资源回收了
super.onDetachedFromWindow();
if (null != mProgressDrawable) {

mProgressDrawable.stop();
}
}
}

 


方式4:直接继承现有控件,对现有控件扩展,类似继承与view (但可预览);系统自带的所有控件都是继承与view,里面实现onDraw()方法
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);
}
}

 

 

注意:

1 若要抛出和暴露结果,可以结合回调函数使用

2 可以参考系统自带的控件 源码,查看相关方法,更容易看明白自定义控件的方式。

 

posted @ 2017-08-03 11:39  JavAndroidJSql  阅读(852)  评论(0编辑  收藏  举报