我正在为 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 参数的构造函数。
关于android - 自定义ImageView崩溃程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5667583/