【发布时间】:2015-02-24 19:16:23
【问题描述】:
我开始了一个针对 Android Lollipop (21) 的项目,并创建了一个自定义视图。当我为视图生成构造函数时,我得到了一个新的第四个构造函数,它比其他构造函数需要更多的参数。
public class FooView extends FrameLayout {
public FooView(Context context) {
super(context);
}
public FooView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FooView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
// This 4th constructor
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public FooView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
}
我的问题是,我们为什么需要它?如果我删除这个构造函数并在 Lollipop 上运行应用程序会发生什么?
【问题讨论】:
-
你不需要它,如果你删除它也不会发生任何事情(参见前面的评论)。它存在于默认视图中,因此您可以传入默认样式资源(请参阅下面的答案:文档)。
-
为什么会有人这样做?
标签: java android view android-5.0-lollipop