android 字定义GridView 引用自己的定义布局

来源:互联网 发布:二级备案域名免费注册 编辑:程序博客网 时间:2024/06/05 15:41

如果你这个自定义的 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文件中的控件进行操作!!!

原创粉丝点击