Android中自定义控件

自定义控件的一个要注意的问题是,自定义控件必须要实现一个参数列表为(Context context, AttributeSet attrs, Map inflateParams)的构造方法。否则将会在引用页面时抛出一个类似于An error has occured in Process com.android.Shell. Unable to start activity ComponentInfo(com.android.Shell/com.android.Shell.StartShell|;
android.view.ViewInflate$InflateException: Binary XML file Line# 7: Error inflating class CoolView

的异常。原因可能是系统会自动调用自定义控件类中拥有该三个参数的构造方法。如果不定义,则系统无法找到

然后在页面中以class的方式引用即可。如

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

/>
</LinearLayout>

相关文章: