在Android中扩展LinearLayout时如何将其他参数传递给构造函数?

use*_*939 1 android constructor class extend android-linearlayout

我有以下问题,如果我有一个扩展LinearLayout的类,如:

public class ExtendedSeekbarLayout extends LinearLayout { ..}
Run Code Online (Sandbox Code Playgroud)

我想将其他参数传递给我的版面,我该怎么做?我知道我可以拥有以下构造函数:

 public ExtendedSeekbarLayout (Context context) {
    super(context);

}

public ExtendedSeekbarLayout (Context context, AttributeSet attributeSet) {
    super(context, attributeSet);

}

public ExtendedSeekbarLayout (Context context, AttributeSet attributeSet, int defStyle) {
    super(context, attributeSet, defStyle);

}
Run Code Online (Sandbox Code Playgroud)

但我希望有类似的东西:

 public ExtendedSeekbarLayout (Context context, AttributeSet attributeSet, int defStyle, int position) {
    super(context, attributeSet, defStyle);
    init(position);

}
Run Code Online (Sandbox Code Playgroud)

我不确定这是否可能,如果没有,那将是什么方式呢?

非常感谢和欢呼,pingu

Moh*_*had 5

共享的此构造函数应该完全按预期工作.

public ExtendedSeekbarLayout (Context context, AttributeSet attributeSet, int defStyle, int position) {
    super(context, attributeSet, defStyle);
    init(position);
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句,你不一定需要这个构造函数,只要你打电话

super(context);
Run Code Online (Sandbox Code Playgroud)

您可以在以编程方式实例化视图的情况下执行此操作:

public ExtendedSeekbarLayout (Context context, int position) {
    super(context);
    init(position);
}
Run Code Online (Sandbox Code Playgroud)

但是,如果您正在讨论从xml发送自定义值,而您实际上没有调用构造函数,那么您应该看看这个答案:https: //stackoverflow.com/a/7608739/2534007