如果你这个自定义的 view要在xml中使用,那么必须使用构造方法含有两个参数或者两个参数以上的。 否则一个参数的构造方法就可以。
在构造方法中使用 inflate(context, Rlayout.main_activity.xml, GripView)
public class CustomView extends ViewGroup {
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
inflate(context, R.layout.activity_main, this);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}
调用了这个方法之后就可以直接在view中findViewById去寻找对应xml文件中的控件进行操作!!!