我自己写了个类继承RelativeLayout
public class ClientLayout extends RelativeLayout
{
public ClientLayout(Context context)
{
super(context);
}
public ClientLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint=new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
canvas.drawRect(0, 0, 100, 100, paint);
}
}
然后再Activity中创建。
ClientLayout layout=new ClientLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
setContentView(layout);
可是无法画出红色的矩形来啊,从View继承就可以画出来,这是为什么啊?
------解决方案--------------------
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint=new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
canvas.drawRect(0, 0, 100, 100, paint);
}
尝试把这段放在dispatchDraw里面。