2 回答
TA贡献10条经验 获得超1个赞
//测量
@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); //设置宽高 即正方形布局
- 2 回答
- 0 关注
- 1546 浏览
添加回答
举报