OnDraw中没有得到所谓的从视图布局派生的自定义视图 [英] onDraw not getting called for a custom view derived from view in layout

查看:108
本文介绍了OnDraw中没有得到所谓的从视图布局派生的自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义视图
.........

I have a custom view .........

package com.yasir.canvasTest;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.util.Log;
import android.view.View;


public class GraphView extends View {

    private static final String TAG = "GraphView";
    public static boolean BAR = true;
    public static boolean LINE = false;

    private Paint paint;
    private float[] values;
    private String[] horlabels;
    private String[] verlabels;
    private String title;
    private boolean type;
    public Canvas tmpCanvas = null;

    public GraphView(Context context, float[] values, String title, String[] horlabels, String[] verlabels) {
        super(context);
        //setScrollContainer(true);
        Log.v(TAG,"On GraphView==>");




        paint = new Paint();

        setWillNotDraw(false);
        requestLayout();
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {




        Log.v(TAG,"<==On Draw");

/// drawing code here
        invalidate();
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.v(TAG,"On onMeasure==>");
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(
                parentWidth * 2, parentHeight);
    }

}

...............
活动:的onCreate
...............

............... Activity: onCreate ...............

setContentView(R.layout.horscroll);

HorizontalScrollView hsView = (HorizontalScrollView) findViewById(R.id.hzsv);

LinearLayout ll = (LinearLayout) findViewById(R.id.linearParent);
ll.removeAllViews();


// create custom view and add to laywout
GraphView graphView = new GraphView(this, values, "GraphViewDemo",horlabels, verlabels, GraphView.LINE);


hsView.requestLayout();
hsView.setWillNotDraw(false);

ll.requestLayout();
ll.setWillNotDraw(false);
ll.addView(graphView);

.....................
XML
....................

..................... Xml ....................

<?xml version="1.0" encoding="utf-8"?>

<HorizontalScrollView
 android:id="@+id/hzsv"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:id="@+id/linearParent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
 </HorizontalScrollView>

..................

..................

问候,
亚西尔·佩尔韦

Regards, Yasir Perwez

推荐答案

试着用层次浏览器这是怎么回事检查。我怀疑你的观点是没有得到画,因为它有一个0宽度,这可能是出于两个原因(一个或两个):

Try checking with Hierarchy Viewer what's going on. I suspect your view is not getting drawn because it has a 0 width, and this could be for two reasons (either or both):


  • 你没有设置任何 LinearLayout.LayoutParams 在自定义视图将它添加到布局之前,我不知道,如果默认的是的填写父之类;

  • onMeasure()是不完整的,应该考虑到还是模式( UNSPECIFIED 完全 AT_MOST )至极,将取决于从布局PARAMS了。有没有保证的getSize()返回父母的大小,作为框架意味着,你应该甚至不知道如果出现这种情况。

  • you're not setting any LinearLayout.LayoutParams on your custom view before adding it to the layout, and I'm not sure if the default ones are of the "fill parent" kind;
  • your onMeasure() is incomplete, it should take into account also the mode (UNSPECIFIED, EXACTLY, AT_MOST) wich will depend from the layout params too. There's no guarantee that getSize() returns parent's size, and as the framework is meant, you shouldn't even know if that happens.

(@ slund的建议将可能是太有用)

(@slund's suggestion will probably be useful too)

这篇关于OnDraw中没有得到所谓的从视图布局派生的自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆