首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ShapeDrawable插件LinearLayout不能无用

ShapeDrawable插件LinearLayout不能无用
EN

Stack Overflow用户
提问于 2015-11-03 13:48:06
回答 1查看 31关注 0票数 0

我已经创建了DotView扩展View,例如:

代码语言:javascript
复制
 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

代码语言:javascript
复制
 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。

EN

回答 1

Stack Overflow用户

发布于 2015-11-03 14:28:59

我解决了这个问题。太愚蠢了,应该使用getMeasuredWidth()getWidth()是0

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33492212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档