我有一个简单的自定义视图,我正在务实地添加到布局中。视图不绘制。来自onMeasure
和onLayout
的值表明视图没有绘制,因为它没有高度。我尝试使用布局参数的常量应用视图,但这并没有解决问题。我尝试设置布局参数的不同组合,覆盖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);
}
}
如果有人知道深入了解Android
View 系统如何工作的好资源,请逐步说明如何避免这些问题;我将不胜感激。
谢谢,