Android中自定义控件-正方形视图横竖屏适配

有时候我们可能有个自定义控件,需要在横屏和竖屏的情况下都可以正常显示全。那么我们就要重写它的onMeasure方法。

@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);
    }
}

这样当我们横竖屏切换的时候,就可以保证视图一直都是可以显示完全的。

猜你喜欢

转载自blog.csdn.net/weixin_38322371/article/details/113656953
今日推荐