Android控件属性有最小宽度minWidth,但没有最大宽度maxWidth,那就自定义View搞定它

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

 

上一篇:flutter 父级宽度不固定,子集多个元素自动填充的布局方案方法


下一篇:(八十八)c#Winform自定义控件-转子