当前位置: 代码迷 >> Android >> 新手,怎么重绘RelativeLayout
  详细解决方案

新手,怎么重绘RelativeLayout

热度:149   发布时间:2016-05-01 22:28:25.0
新手,如何重绘RelativeLayout?
我自己写了个类继承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里面。
  相关解决方案