自定义View的注意点

最近在家无事,开始学习android。

自己做了一个自定义的View,布署到main.xml上去,却发现跑的时候老是出现错误。

后来发现,原来是集成View的时候没有写上父类3个参数的构造方法。

估计其内部默认情况下会调用那个构造方法吧。

 

 

贴上代码:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="fill_parent">
	<com.ant.memory.PhotoView
		android:id="@+id/photo" android:layout_width="fill_parent"
		android:background="#0000ff"
		android:layout_height="fill_parent" tileSize="24" />
</FrameLayout>

 

PhotoView:

public class PhotoView extends View {
	/*
	 * 这个constructor是必须的要的,如果你想将这个View布署道main.xml上
	 */
	public PhotoView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	@Override
	public void draw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.draw(canvas);
		
		InputStream is=getResources().openRawResource(R.drawable.back);
		Bitmap bitmap=BitmapFactory.decodeStream(is);
		Paint paint=new Paint();
		canvas.drawBitmap(bitmap, 50, 50, paint);
	}
}

 

 

Memory.java:

package com.ant.memory;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class Memory extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        PhotoView photoView;
        setContentView(R.layout.main);
        photoView=(PhotoView) findViewById(R.id.photo);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值