我有一个扩展名为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>
如果包含完整的堆栈跟踪,您可能会有更好的运气获得帮助。 – CommonsWare 2010-07-10 20:57:00