其他分享
首页 > 其他分享> > Android控件属性有最小宽度minWidth,但没有最大宽度maxWidth,那就自定义View搞定它

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

 

标签:控件,MeasureSpec,val,maxWidth,min,Int,宽度,context,自定义
来源: https://www.cnblogs.com/yongfengnice/p/11468979.html