如图,第一次用自定义继承listview的写法,不是很理解,之前都用布局用的。如何理解这个三个方法,一定要写全吗?
public class DefineListView extends ListView {
public DefineListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public DefineListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DefineListView(Context context) {
super(context);
}
在java代码中直接new一个DefineListView ,会调用一个参数的构造函数,比如DefineListView defineListView = new DefineListView (context)
。
如果是在xml中创建这个控件并且没有指定style,会调用两个参数的,如
<com.example.DefineListView
android:layout_width="match_parent"
android:layout_height="wrap_content"></com.example.DefineListView>
如果在xml中创建并设置了style,就会调用三个参数的。
<com.example.DefineListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/myStyle"></com.example.DefineListView>