开发者

How can I scroll custom view in Android

开发者 https://www.devze.com 2023-03-13 04:34 出处:网络
Sorry, my english is bad, but I need in your help men. I created a custom view package com.gwprogram; import android.content.Context;

Sorry, my english is bad, but I need in your help men. I created a custom view package 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
    } 
}

In Activity I created this code:

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.setBackgr开发者_如何学PythonoundColor(Color.WHITE);
            setContentView(canvView);
            NoteSheet sheet=new NoteSheet(canvView);
            sheet.draw();        
        }
    }

But image is very large and I need in scroll view for my DrawCanvasView. Help me, I don't know how to create scroll view in my situation. Thank you.


You can do it adding something like this in your xml:

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

Or you can do it as CaspNZ said.


Add a ScrollView as the top level parent of the view like so:

@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);
}

You could do this in xml, you know...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号