- 时间:2022-06-30 20:25 编辑: 来源: 阅读:
- 扫一扫,手机访问
摘要:Android实现在xml文件中引用自定义View的方法分析
本文实例讲述了Android实现在xml文件中引用自定义View的方法。分享给大家供大家参考,具体如下:
在xml中引用自定义view
[b]方法一:[/b]
<view class="com.test.copytext.CopyText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
[b]方法二:[/b]
<view class="com.test.copytext.CopyText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="aaaaaaaaaaaa"
/>
在xml文件中间引用自定义view时遇到了这个错误:
Java.lang.RuntimeException: Unable to start activity ComponentInfo{cm.test/cm.test.TestsActivity}: Android.view.InflateException: Binary XML file line #8: Error inflating class com.test.testview
这就需要查看在xml中引用时的包名是否正确
还可能会遇到这个错:
11-24 10:58:38.993: ERROR/AndroidRuntime(323): Caused by: java.lang.NoSuchMethodException: HelloView(Context,AttributeSet)
这是因为自定义View中缺少了一个构造方法
public HelloView(Context context,AttributeSet attrs){
super(context, attrs);
}
更多关于Android相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/375.htm]Android视图View技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/371.htm]Android布局layout技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/410.htm]Android开发入门与进阶教程[/url]》、《[url=http://www.1sucai.cn/Special/508.htm]Android调试技巧与常见问题解决方法汇总[/url]》、《[url=http://www.1sucai.cn/Special/381.htm]Android基本组件用法总结[/url]》及《[url=http://www.1sucai.cn/Special/124.htm]Android控件用法总结[/url]》
希望本文所述对大家Android程序设计有所帮助。