Android(安卓)自定义View引用xml布局
16lz
2021-12-04
在构造方法中调用方法:
inflate(context, R.layout.activity_main, this);
一参 Context上下文
二参 xml布局文件id
三参 ViewGroup
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文件中的控件进行操作
另:
如果需要在xml文件中引用自定义view需要重新两参以上构造方法,如果不需要再xml文件中引用,则只需要重新一参构造方法即可
更多相关文章
- 浅谈Java中Collections.sort对List排序的两种方法
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- python list.sort()根据多个关键字排序的方法实现
- android一些认识
- android 点击返回键退出程序的方法
- 在android中创建bitmap避免内存不足的方法
- Android(安卓)studio+真机 运行报错[INSTALL_FAILED_INSUFFICIEN