Android layout with square buttons

前端 未结 6 755
感动是毒
感动是毒 2020-11-30 09:31

I want to make a layout similar to this one:

www.ImageBanana.net - layout.png http://www.imagebanana.com/img/9kmlhy66/thumb/layout.png

Four square buttons on

6条回答
  •  感动是毒
    2020-11-30 09:53

    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.LinearLayout;
    
    public class SquareLayout extends LinearLayout {
        // Desired width-to-height ratio - 1.0 for square
        private final double mScale = 1.0;
    
        public SquareLayout(Context context) {
            super(context);
        }
    
        public SquareLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = MeasureSpec.getSize(heightMeasureSpec);
    
            if (width > (int)((mScale * height) + 0.5)) {
                width = (int)((mScale * height) + 0.5);
            } else {
                height = (int)((width / mScale) + 0.5);
            }
    
            super.onMeasure(
                    MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
            );
        }
    }
    

提交回复
热议问题