通常自定義VIewGroup步驟:
1.繼承ViewGroup
2.重寫onMeasure
3.重寫onLayout
4.重寫onDrawjava
此時,MarginLayoutParams cParams = (MarginLayoutParams) view1.getLayoutParams();會報異常
異常內容爲:android
java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams
因此要支持Margin還有必須 重寫generateLayoutParams方法
實例 :markdown
@Override public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { return new MarginLayoutParams(getContext(), attrs); }
這樣就能夠支持Margin 進行操做了ide