2011-06-14 47 views
1

對不起,我的英語不好,但我需要你的幫助男士。我創建了一個自定義視圖 package com.gwprogram;如何在Android中滾動自定義視圖

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 
    } 
} 

在活動我創造了這個代碼:

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。幫助我,我不知道如何在我的情況下創建滾動視圖。謝謝。

回答

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中,你知道這樣做......

2

你可以將其添加這樣的事情在你的xml:

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

或者你可以這樣做,因爲CaspNZ說。