Custom controls in Android-square view horizontal and vertical screen adaptation

Sometimes we may have a custom control that needs to be displayed normally in both horizontal and vertical screens. Then we have to rewrite its onMeasure method.

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = View.MeasureSpec.getSize(widthMeasureSpec);
    int height = View.MeasureSpec.getSize(heightMeasureSpec);
    if (width < height){
        super.onMeasure(widthMeasureSpec,widthMeasureSpec);
    }else{
        super.onMeasure(heightMeasureSpec,heightMeasureSpec);
    }
}

In this way, when we switch between horizontal and vertical screens, we can ensure that the view can always be displayed completely.

Guess you like

Origin blog.csdn.net/weixin_38322371/article/details/113656953