Android:自定义视图不绘图

时间:2013-04-23 18:58:37

标签: android view

我的部分Android应用程序出现问题。我需要在自定义对话框中绘制图形,但即使我遵循互联网上的所有解决方案,它也不起作用。我的目标是在用户点击主框架上的按钮时显示一个对话框。为此,我想在一个非常简单的对话框中绘制它。我的图形需要“可滚动”,因为它可能比对话框大。以下是来源代码:

public class GraphDialog extends Dialog implements android.view.View.OnClickListener
{
    Canvas canvas;

    public GraphDialog(Context context) 
    {
        super(context);
        System.out.println("Test");
        this.setContentView(R.layout.dialog_graph_layout);
        this.setTitle(R.string.title_dialog_graph);
        ((Button) this.findViewById(R.id.button_ok)).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) 
    {
        Controller.getPollManager().setPoll(null);
        Controller.getPollManager().setTab(null);
        this.cancel();
    }
}

这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <ScrollView
        android:id="@+id/scroll_layout"
        android:layout_width="500px"
        android:layout_height="800px"
        android:layout_weight="1"
        android:drawingCacheQuality="low"
        android:scrollbars="vertical" >

        <be.ac.ucl.lfsab1509.proxipoll.GraphView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

        </be.ac.ucl.lfsab1509.proxipoll.GraphView>
    </ScrollView>

    <Button
        android:id="@+id/button_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0"
        android:text="@string/ok" />

</LinearLayout>

这是我的自定义视图:

public class GraphView extends View
{
    Paint paint;

    public GraphView(Context context) 
    {
        super(context);
        System.out.println("test GraphView");
        this.setWillNotDraw(false);
        init();
    }

    public GraphView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        System.out.println("test GraphView");
        this.setWillNotDraw(false);
        init();
    }

    public void init()
    {
        paint = new Paint();

        paint.setColor(Color.BLACK);
    }

    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        System.out.println("test onDraw");
        canvas.drawText(Controller.getPollManager().getPoll().name, 50, 50, paint);
        this.draw(canvas);
    }
}

要查看出错的地方,您可以看到我添加了一些println()。当我尝试显示对话框时,除了onDraw()方法的on之外,我得到所有行。有人知道该怎么做才能让它发挥作用吗?

谢谢

1 个答案:

答案 0 :(得分:3)

尝试覆盖onMeasure:当我上次做任何自定义图表时,我需要做这样的事情。

@Override protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec )
    {
    viewWidth = MeasureSpec.getSize( widthMeasureSpec );
    viewHeight = MeasureSpec.getSize( heightMeasureSpec );

    if(box.getWidth() != 0){
        // use the screen width: oldViewWidth -> screenWidth, viewWidth -> maxXRange
        boxWidth = box.getWidth() - box.getPaddingRight() - box.getPaddingLeft() - 10;
        if(screenWidth <= 0) viewWidth = boxWidth;
        else viewWidth = (int)((getMaxXRange()*boxWidth)/screenWidth);
        if(viewWidth < boxWidth) viewWidth = boxWidth;
    }
    setMeasuredDimension( viewWidth, viewHeight );
    }

这基本上会检查窗口的大小,并告诉图形层我想要它有多大。