Android中如何实现一个宽和高都等于【屏幕宽度】的正方形控件?

肆意先生
浏览 1554回答 2
2回答

Js_Ly

//测量    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int widthSize = MeasureSpec.getSize(widthMeasureSpec);        int widthMode = MeasureSpec.getMode(widthMeasureSpec);        int highSize = MeasureSpec.getSize(heightMeasureSpec);        int highMode = MeasureSpec.getMode(heightMeasureSpec);        int width = Math.min(widthSize,highSize); //获取最小值来实现正方形布局        //防止出现尺寸为0不显示        if (widthMode == MeasureSpec.UNSPECIFIED){            width = highSize;        }else if (highMode == MeasureSpec.UNSPECIFIED){            width = widthSize;        }        setMeasuredDimension(width, width); //设置宽高 即正方形布局
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android