android自定义控件 几种方式总结
方式1:不繼承任何組件 , 直接在代碼里面調(diào)用實(shí)例化。
public class ProgressDialog {
private Dialog dialog;
public ProgressDialog(@NonNull Context context) { //構(gòu)造函數(shù)必須有
dialog = new Dialog(context);
buildDialog(context);
}
……其他邏輯方法
}
?
方式2:單獨(dú)控件,繼承與View ,可以在xml上調(diào)用(無法預(yù)覽,因?yàn)樾枰谶\(yùn)行時候才onDraw繪制)
public class ProgressPieView extends View {
public ProgressPieView(Context context) {
this(context, null);
}
public ProgressPieView(Context context, AttributeSet attrs) {//必須添加有AttributeSet 的構(gòu)造函數(shù),才能在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() {//布局 附加到主 布局視圖中時會執(zhí)行;不用再手動資源回收了
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {//布局 從主布局移除會執(zhí)行;不用再手動資源回收了
super.onDetachedFromWindow();
}
……其他邏輯方法
}
?
方式3:組合控件,里面有多個控件的,繼承與Linerlayout或者RelatityLayout或FrameLayout;可在xml上直接編寫(可預(yù)覽)
;;;layout布局本身就是繼承與 ViewGroup ViewGroup ViewGroup!
public class GirdMenuView extends FrameLayout {
private RecyclerView mRecyclerView;
private List<CategoriesModel> datas = new ArrayList<>();
//執(zhí)行加載,xml布局的時候,就會執(zhí)行構(gòu)造函數(shù)
public GirdMenuView(@NonNull Context context) {
this(context, null);
}
public GirdMenuView(@NonNull Context context, @Nullable AttributeSet attrs) {必須添加有AttributeSet 的構(gòu)造函數(shù),才能在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) {//控制控件手勢沖突 和 高度,不是必須設(shè)置~
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
……
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {//加載布局時候,控制位置
……
}
@Override
protected void onAttachedToWindow() {//布局 附加到主 布局視圖中時會執(zhí)行;不用再手動資源回收了
super.onAttachedToWindow();
if (mProgressDrawable != null) {
mProgressDrawable.start();
}
}
@Override
protected void onDetachedFromWindow() {//布局 從主布局移除會執(zhí)行;不用再手動資源回收了
super.onDetachedFromWindow();
if (null != mProgressDrawable) {
mProgressDrawable.stop();
}
}
}
?
方式4:直接繼承現(xiàn)有控件,對現(xiàn)有控件擴(kuò)展,類似繼承與view (但可預(yù)覽);系統(tǒng)自帶的所有控件都是繼承與view,里面實(shí)現(xiàn)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 若要拋出和暴露結(jié)果,可以結(jié)合回調(diào)函數(shù)使用
2 可以參考系統(tǒng)自帶的控件 源碼,查看相關(guān)方法,更容易看明白自定義控件的方式。
?
轉(zhuǎn)載于:https://www.cnblogs.com/softwarelanguagebs/p/7278847.html
總結(jié)
以上是生活随笔為你收集整理的android自定义控件 几种方式总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JDBC(11)—数据库连接池
- 下一篇: fiddler 抓取手机app请求包