9
我开始了一个针对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);
}
}
我的问题是,我们为什么需要它?如果我删除此构造函数并在棒棒糖上运行应用程序,会发生什么情况?
看到https://stackoverflow.com/questions/9195713/do-i-need-all-three-constructors-for-an-android-custom-view –
你不需要它并没有什么将会发生,如果你删除它(见前面的评论)。它存在于默认视图中,以便您可以传入默认样式资源(请参阅下面的answer re:docs)。 – alanv
为什么有人想这样做? – mbonnin