Android 自定义View引用xml布局
16lz
2021-01-23
在构造方法中调用方法:
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文件中引用,则只需要重新一参构造方法即可
更多相关文章
- android颜色设使用方法
- android 点击返回键退出程序的方法
- Android studio+真机 运行报错[INSTALL_FAILED_INSUFFICIENT_STO
- Android——Android Studio开发环境搭建及设置本地Gradle方法
- 在android中创建bitmap避免内存不足的方法
- Android Studio使用Wifi调试的方法:使用Android WIFI ADB
- 【Android动态布局】之【使用addView方法时,如何保持已有动态控件