標籤:注意 onmeasure hellip dialog 調用 sim group 載入 ble
方式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 可以參考系統內建的控制項 源碼,查看相關方法,更容易看明白自訂控制項的方式。
android自訂控制項 幾種方式總結