在android中添加视图到scrollview

时间:2012-07-19 19:14:22

标签: android view scrollview gif ondraw

我在scrollview中有一个相对布局,然后用这段代码创建了一个新视图:

public class DrawView extends View {
    Paint paint = new Paint();

    public DrawView(Context context) {
        super(context);            
    }

    @Override
    public void onDraw(Canvas canvas) {
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(2);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(5, 5, 140, 140, paint);

    }
}

然后我尝试使用layout.addView(DrawView)将新视图添加到相对布局,因此它可以与其余内容一起滚动,但是不起作用,什么都没有显示..

我错过了什么吗?

编辑:

DrawView formas;
GifMovieView gif;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final RelativeLayout layout = new RelativeLayout(this);
    final ScrollView scrollView = new ScrollView(this);
    //gif = new GifMovieView(this, t_img);
    formas = new DrawView(this);

    layout.addView(formas);
    scrollView.addView(layout);


    this.setContentView(scrollView);    
    inicializar();
    load(layout);   
}

1 个答案:

答案 0 :(得分:2)

您可能希望在DrawView中覆盖onMeasure()。否则,您的视图将具有0x0像素的大小。

你可以从像

这样简单的事情开始
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(200, 200);
}

只是为了证明它有效,但你需要根据视图的内容做一些更有意义的事情。

this article开始,查看覆盖onDraw()onMeasure()

的帮助
相关问题