我正在为 Android 制作一个小型应用程序,其中有一个relativelayout,其中包含一个自定义 ImageView。在我的 Java 代码中,我有这个类:
package com.example.android.helloactivity;
class ArrowImageView extends ImageView {
public ArrowImageView(Context context) {
super(context);
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(10,10,10,null);
}
}
然后在我的RelativeLayout xml 中我有以下内容:
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFF"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button ...... />
<TextView ......./>
<com.example.android.helloactivity.ArrowImageView
android:id="@+id/hello_activity_bearingarrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
当我运行我的主类(此处未显示)时,我的程序崩溃了。如果我省略对 ArrowImageView 的 xml 引用,那么它不会崩溃。
我是否以错误的方式引用我的自定义类,或者发生了什么?
扩展 View 小部件时,如果您计划在 XML 布局中使用它们,则还需要重写采用 AttributeSet 参数的构造函数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)