Android实现在xml文件中引用自定义View的方法分析
本文实例讲述了Android实现在xml文件中引用自定义View的方法。分享给大家供大家参考,具体如下:
在xml中引用自定义view
方法一:
<view class="com.test.copytext.CopyText" android:layout_width="fill_parent" android:layout_height="wrap_content" />
方法二:
<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相关内容感兴趣的读者可查看本站专题:《Android视图View技巧总结》、《Android布局layout技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
相关文章
Android 系统服务TelecomService启动过程原理分析
这篇文章主要介绍了Android 系统服务TelecomService启动过程原理分析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-07-07Android Studio 3.6中使用视图绑定替代 findViewById的方法
从 Android Studio 3.6 开始,视图绑定能够通过生成绑定对象来替代 findViewById,从而可以帮您简化代码、移除 bug,并且从 findViewById 的模版代码中解脱出来,今天通过本文给大家介绍使用视图绑定替代 findViewById的方法,感兴趣的朋友一起看看吧2020-03-03Android App开发中HTTP扩展包OkHttp的入门使用指南
OkHttp包为安卓开发中基于HTTP协议的网络编程提供了很大便利,这里我们就来看一下Android App开发中HTTP扩展包OkHttp的入门使用指南:2016-07-07Android Studio 3.0 gradle提示版本太老
这篇文章主要介绍了Android Studio 3.0 gradle提示版本太老的配置和解决方法。2017-11-11Android TV开发:使用RecycleView实现横向的Listview并响应点击事件的代码
这篇文章主要介绍了Android TV开发:使用RecycleView实现横向的Listview并响应点击事件的代码,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧2018-05-05
最新评论