本篇博客是文章的繼續。
3. LayoutParams
①:此代碼知識點較多,另一篇博客會專門記錄。
//Margin是子view相對父控件四條邊的距離
@Override
public ViewGroup.MarginLayoutParams generateLayoutParams(AttributeSet attrs){
return new MarginLayoutParams(getContext(), attrs);
}
三、注意事項
1. 如果需要從XML文件中inflate控件,必須寫出控件以下全部構造方法。否則會報java.lang.classnotfoundexception。
//構造方法
public CricleMenu(Context context) {
super(context);
}
public CricleMenu(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CricleMenu(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
<com.example.custom_view_test.CricleMenu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#AA333333">
</com.example.custom_view_test.CricleMenu>