本文實(shí)例講述了Android實(shí)現(xiàn)在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)
這是因?yàn)樽远xView中缺少了一個構(gòu)造方法
public HelloView(Context context,AttributeSet attrs){ super(context, attrs);}希望本文所述對大家Android程序設(shè)計有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選