【UI】【View】自定義佈局方法與注意事項(二)

本篇博客是文章的繼續。

【UI】【View】自定義佈局方法與注意事項(一)


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);
	}


2.在使用自定義view時,XML文件要引用全部包名,否則會報android.view.inflateException。

<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>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章