我已经创建了DotView扩展View,例如:
private ShapeDrawable mDrawable = new ShapeDrawable(new OvalShape());
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
mWidth = getWidth();
mHeight = getHeight();
if(mWidth>=mHeight){
circleDiameter=mHeight/3;
}else{
circleDiameter=mWidth/3;
}
left = (mWidth - circleDiameter) / 2;
top = (mHeight - circleDiameter) / 2;
right = left + circleDiameter;
bottom = top + circleDiameter;
super.onMeasure (widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
canvas.drawLine (0, 0, 0, this.getHeight () - 1, paint);
canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, this.getHeight() - 1, paint);
canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1, this.getHeight() - 1, paint);
mDrawable.setBounds (left, top, right, bottom);
mDrawable.getPaint().setColor (Color.BLACK);
mDrawable.draw (canvas);
}只需在xml中使用DotView即可显示:

然后我创建一个PasswordBox扩展LinearLayout并添加六个DotView
private int mPwdSize = 6;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure (widthMeasureSpec, heightMeasureSpec);
setOrientation (HORIZONTAL);
for(int i = 0; i < mPwdSize; i++) {
DotView dotView = new DotView (getContext ());
LayoutParams layoutParams = new LayoutParams (getWidth ()/6,getHeight ());
addView (dotView, layoutParams);
}
}但是它不起作用!ShapeDrawable无法显示。看图片:

我不知道代码在哪里出了问题,请帮助me.thanks。
发布于 2015-11-03 14:28:59
我解决了这个问题。太愚蠢了,应该使用getMeasuredWidth(),getWidth()是0
https://stackoverflow.com/questions/33492212
复制相似问题