自定义继承listview的三个重写的方法怎么理解

如图,第一次用自定义继承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);
    }
阅读 5.2k
1 个回答

在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>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题