2010-07-10 73 views
3

我有一个扩展名为NoteView的FrameLayout的类。 这是一个顶级的公共课。如何在XML中使用自定义的Android ViewGroup?

它工作得很好,当我从Java代码创建UI时,但我无法弄清楚如何在XML中使用它。我尝试插入完全限定的类名称,就像我为自定义视图一样,但当活动试图膨胀XML时,我得到并例外ClassCastException: android.view.View cannot be cast to android.view.ViewGroup

我只能在自定义视图上找到教程,而不是自定义视图组。我需要做什么才能像使用XML布局中的FrameLayout一样使用NoteView?

例如:该作品

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<test.cjb.sandbox.NoteView 
android:id="@+id/noteview" 
android:layout_width="fill_parent" 
android:layout_height="100dp" 
android:background="#FF0000" 
android:layout_gravity="right"> 
</test.cjb.sandbox.NoteView> 
    <Button 
    android:id="@+id/button" 
    android:layout_width="100dp" 
    android:layout_height="fill_parent" 
    android:text="Sibling Button"/> 
</LinearLayout> 

但是,这将引发上述异常:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
<test.cjb.sandbox.NoteView 
android:id="@+id/noteview" 
android:layout_width="fill_parent" 
android:layout_height="100dp" 
android:background="#FF0000" 
android:layout_gravity="right"> 
    <Button 
    android:id="@+id/button" 
    android:layout_width="100dp" 
    android:layout_height="fill_parent" 
    android:text="Child Button"/> 
</test.cjb.sandbox.NoteView> 

</LinearLayout> 
+0

如果包含完整的堆栈跟踪,您可能会有更好的运气获得帮助。 – CommonsWare 2010-07-10 20:57:00

回答

2

在未来,抱怨异常时,请考虑包括完整的堆栈跟踪,尤其是人的时候请求完整的堆栈跟踪,因为完整的堆栈跟踪可能包含与给出答案有关的信息。您可以通过adb logcat,DDMS或Eclipse中的DDMS透视图获得完整的堆栈跟踪。

由于缺乏这一点,我所能做的就是将您指向this sample project,将LinearLayout扩展为自定义小部件。

相关问题