0

我有一个简单的自定义视图,我正在务实地添加到布局中。视图不绘制。来自onMeasureonLayout的值表明视图没有绘制,因为它没有高度。我尝试使用布局参数的常量应用视图,但这并没有解决问题。我尝试设置布局参数的不同组合,覆盖onMeasure, onDraw, onLayout, setting setMeasuredDimension, settingsetWillNotDraw为 false,但似乎没有任何效果。

附加此视图的布局设置为包装内容,并在适配器中创建。我已经发布了我的源代码。

private void addGuessNumeral(RowHolder row) {       
    LinearLayout.LayoutParams numeral_view_params = new LinearLayout.LayoutParams(100, 100); 

    NumeralView numeral_view = new NumeralView(context);
    numeral_view.setLayoutParams(numeral_view_params);
    Log.d("MyGamesAdapter", "row.game.getGuesses.length = " + row.game.getGuesses().length);
    numeral_view.setCount(row.game.getGuesses().length);

    row.layout_numeral.addView(numeral_view);       
}

public class NumeralView extends View {

    private static final int BAR_SPACING = 5;
    private static final int BAR_WIDTH = 5;
    private static final int BAR_HEIGHT = 15;
    private static final int PADDING = 5;

    private static final String TAG = "NumeralView";

    private int count;
    private Paint paint;
    private int width;
    private int height;

    public NumeralView(Context context) {
        super(context);

        paint = new Paint (Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(BAR_WIDTH);

        setWillNotDraw(false);          
    }

    public void setCount(int count) {           
        this.count = count;

    //  this.setLayoutParams(new LayoutParams((PADDING*2) + (count * (BAR_SPACING + BAR_WIDTH)), 
    //          BAR_HEIGHT + (PADDING*2)));
    }

    @Override
    protected void onDraw(Canvas canvas) {          
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

        for(int i = 0; i < count; i++) {
            //algorithm: each bar will occupy on tenth of the width.  Bars are spaced with one tenth of the width apart
            canvas.drawLine(i * (BAR_SPACING + BAR_WIDTH), 0, i * (BAR_SPACING + BAR_WIDTH) + BAR_WIDTH,  height, paint);               
            if (i%5 == 0) {                 
                canvas.drawLine(0, 0, width, height, paint);
            }
        }           
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {            
        Log.d("NumeralView", "left = " + left);
        Log.d("NumeralView", "top = " + top);
        Log.d("NumeralView", "right  = " + right);
        Log.d("NumeralView", "bottom = " + bottom);

        height = bottom - top;
        width = right - left;

        Log.d("NumeralView", "height = " + height);
        Log.d("NumeralView", "width = " + width);

        super.onLayout(changed, left, top, right, bottom);
    }

    /* (non-Javadoc)
     * @see android.view.View#onMeasure(int, int)
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {         
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(widthMeasureSpec, MeasureSpec.AT_MOST);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightMeasureSpec, MeasureSpec.AT_MOST);

        Log.d("NumeralView", "widthMeasureSpec = " +  widthMeasureSpec);
        Log.d("NumeralView", "heightMeasureSpec = " +  heightMeasureSpec);

        setMeasuredDimension(heightMeasureSpec, heightMeasureSpec);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /* (non-Javadoc)
     * @see android.view.View#onSizeChanged(int, int, int, int)
     */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);          
    }           
}

如果有人知道深入了解AndroidView 系统如何工作的好资源,请逐步说明如何避免这些问题;我将不胜感激。

谢谢,

4

1 回答 1

0
  1. 如果您调用 setMeasuredDimension ,则不应调用 super.onMeasure ,因为这将调用默认测量。
  2. 在 onMeasure 方法中,一定要保存视图的宽度和高度,以供 onDraw 稍后使用。
  3. 在包含它的视图“查看”其布局参数之前获取视图的宽度和高度是不可能的,因为这些容器需要根据其周围环境决定赋予它的大小。
  4. onLayout - “当此视图应为其所有子级分配大小和位置时调用。” 这意味着它仅适用于包含其他视图 (ViewGroup) 的视图。

有关更多信息,请参见下一个链接:

http://developer.android.com/guide/topics/ui/custom-components.html

http://developer.android.com/guide/topics/ui/how-android-draws.html

于 2012-05-19T12:11:10.350 回答