在构造方法中调用方法:

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文件中引用,则只需要重新一参构造方法即可

更多相关文章

  1. android颜色设使用方法
  2. android 点击返回键退出程序的方法
  3. Android studio+真机 运行报错[INSTALL_FAILED_INSUFFICIENT_STO
  4. Android——Android Studio开发环境搭建及设置本地Gradle方法
  5. 在android中创建bitmap避免内存不足的方法
  6. Android Studio使用Wifi调试的方法:使用Android WIFI ADB
  7. 【Android动态布局】之【使用addView方法时,如何保持已有动态控件

随机推荐

  1. Android 线程间通信------handler
  2. Android ViewFlipper within TabHost for
  3. Android(安卓)Studio技巧 之 自动命名APK
  4. Android之解决androidx.appcompat.widget
  5. android 2 app development
  6. Android遍历SqlLite cursor对象:
  7. [置顶] android调用.net的webservice的例
  8. Android下载文件提示文件不存在。。。 ja
  9. 一个PhoneGap写的小应用
  10. Android播播放完SD卡指定文件夹音乐之后,