Cod*_*ile 5 user-interface android
我有一个扩展 FrameLayout 的类,称为 NoteView。这是一个顶级的公开课。
当我从 java 代码创建 UI 时它工作得很好,但我不知道如何在 XML 中使用它。我尝试插入完全限定的类名,就像插入自定义视图一样,但是ClassCastException: android.view.View cannot be cast to android.view.ViewGroup当活动尝试膨胀 XML 时,出现异常。
我只能找到有关自定义视图的教程,而不是自定义视图组。我需要做什么才能像在 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>
Run Code Online (Sandbox Code Playgroud)
但这会引发上述异常:
<?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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7141 次 |
| 最近记录: |