如何在Android中滚动自定义视图

时间:2011-06-14 20:10:11

标签: android scrollview

抱歉,我的英语很糟糕,但我需要你的帮助。我创建了一个自定义视图 包com.gwprogram;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

public class DrawCanvasView extends View{

    Section[] sections;
    String authorName;
    String title;
    String copyright;

    public DrawCanvasView(Context context) { 
        super(context); 
        setFocusable(true);
    }

    public void setAttributes(Section[] sections, String authorName,
                                String title, String copyright) {
        this.sections=sections;
        this.authorName=authorName;
        this.title=title;
        this.copyright=copyright;
        invalidate();
    }

    @Override 
    protected void onDraw(Canvas canvas) { 
         //Some code for draw sections and other attributes
    } 
}

在Activity中我创建了这段代码:

package com.gwprogram;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ScrollView;
import android.widget.Toast;

    public class Main extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            DrawCanvasView canvView=new DrawCanvasView(this);
            canvView.setBackgroundColor(Color.WHITE);
            setContentView(canvView);
            NoteSheet sheet=new NoteSheet(canvView);
            sheet.draw();        
        }
    }

但是图像非常大,我需要在DrawCanvasView的滚动视图中。帮助我,我不知道如何在我的情况下创建滚动视图。谢谢。

2 个答案:

答案 0 :(得分:2)

你可以在你的xml中添加这样的东西:

 <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content">
<...>
 </ScrollView>

或者你可以像CaspNZ所说的那样做。

答案 1 :(得分:0)

添加ScrollView作为视图的顶级父级,如下所示:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ScrollView scrollView  = new ScrollView(this);
    setContentView(scrollView);

    View view = new View(this);
    view.setBackgroundColor(Color.WHITE);

    scrollView.addView(view);
}

你可以在xml中做到这一点,你知道......