class MaxWidthLinearLayout @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : LinearLayout(context, attrs, defStyleAttr) { override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { val size = MeasureSpec.getSize(widthMeasureSpec) val maxWidth = ScreenUtils.screenWidth(context) - ScreenUtils.dip2px(128f, context) val min = min(size, maxWidth) val measureSpec = MeasureSpec.makeMeasureSpec(min, MeasureSpec.AT_MOST) super.onMeasure(measureSpec, heightMeasureSpec) } }