Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 简单自定义视图不工作_Android - Fatal编程技术网

Android 简单自定义视图不工作

Android 简单自定义视图不工作,android,Android,我正在尝试创建一个简单的自定义视图,但它不起作用。我参考了布局资源文件中的自定义视图类。但它不起作用。谁能告诉我问题出在哪里 public class WriteOnScreenActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVi

我正在尝试创建一个简单的自定义视图,但它不起作用。我参考了布局资源文件中的自定义视图类。但它不起作用。谁能告诉我问题出在哪里

public class WriteOnScreenActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

}

public class CustomView extends View {

    private Paint paint;

    public TouchEventView(Context context) {
        super(context);

        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setTextSize(25);
        paint.setAntiAlias(true);
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    canvas.drawText("Hello World", 5, 30, paint);   
    }

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

       <com.example.touch.CustomView
           android:id="@+id/TouchEventView"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent" />

</RelativeLayout>
公共类WriteOnScreenActivity扩展活动{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
公共类CustomView扩展了视图{
私人油漆;
公共TouchEventView(上下文){
超级(上下文);
油漆=新油漆();
油漆。设置颜色(颜色。红色);
油漆.尺寸(25);
paint.setAntiAlias(真);
}
受保护的void onDraw(画布){
super.onDraw(帆布);
画布.drawText(“Hello World”,5,30,paint);
}
}

将类TouchEventView的定义移动到新文件中

这是解决办法

public class TouchEventView extends View {

    private Paint paint;

    public TouchEventView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setup(context, attrs, defStyle);
    }

    public TouchEventView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setup(context, attrs, 0);
    }

    public TouchEventView(Context context) {
        super(context);

      setup(context, null, 0);

    }

    private void setup(Context context, AttributeSet attrs, int defStyle)
    {

            paint = new Paint();
            paint.setColor(Color.RED);
            paint.setTextSize(25);
            paint.setAntiAlias(true);
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    canvas.drawText("Hello World", 5, 30, paint);   
    }

}

如果我使用构造函数CustomView(上下文上下文,AttributeSet attrs),它就可以工作。但是它应该在没有AttributeSet attrs参数的情况下工作……不是吗?但是它应该使用本教程中描述的单个参数构造函数-啊,我明白了。但您应该调用它。这意味着,如果我想从XML引用我的视图,我必须调用视图类中的所有超类构造函数?您应该包括所有超类构造函数。:)