1, you need to add this kind of code to the constructors of your custom view which must extend ViewGroup, and the parameter attachToRoot must be true. LayoutInflater.from(context).inflate(R.layout.yourLayout, this, true); 2,If you want to add your cu