我的应用程序中的自定义视图绘图问题

时间:2011-10-03 11:13:49

标签: android view custom-view

我的代码在这里出了什么问题。为什么它没有在屏幕上绘制我的自定义视图。

class CustActivty extends Activty{
private ShapeDrawable mDrawable;;
Path path;

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.imagelayout);
CustomView view=new CustomView(getApplicationContext());
RelativeLayout rl=(RelativeLayout)findViewById(R.id.relativeLayout1);
rl.addView(view);
}


class CustomView extends View{

CustomView(Context context){

超级(上下文);     path = new Path();     RectF rec = new RectF(10,10,400,400);

path.addArc(rec,90,180);

mDrawable = new ShapeDrawable(new PathShape(path,400,400));
mDrawable.setBounds(10, 10, 400,400);
mDrawable.getPaint().setColor(0xff74AC23);
}
protected void onDraw(Canvas canvas){
 mDrawable.draw(canvas);

}
}
}

Plz任何有想法的人.Plz帮助。

1 个答案:

答案 0 :(得分:2)

您应指定LayoutParams用于添加到view

RelativeLayout

所以而不只是rl.addView(view)

RelativeLayout.LayoutParams params = //initialise them as you want
rl.addView(view, params);