0
我HAVA創建DotView
延長View
,如:ShapeDrawable在LinearLayout中添加不能無用
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);
}
只使用DotView
在XML可以desplay:
比我創建了一個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。