您的位置:首页 > 移动开发 > Android开发

android 在xml文件中引用自定义View

2016-01-13 16:47 453 查看
在xml中引用自定义view

方法一:

[java]
view plaincopy

<com.test.copytext.CopyText  
 android:layout_width="fill_parent"  
 android:layout_height="wrap_content"  
 />  

方法二:

[java]
view plaincopy

<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);
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: