0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ViewGroupでMaxWidthを実現する

Posted at

ViewGroup には maxWidth / maxHeight は存在しません。素直に onMeasureoverride して実現しましょう。

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.AT_MOST) {
            return super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        }

        measure(
                MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
        )
        setMeasuredDimension(measuredWidth, measuredHeight)
    }

onMeasure 時に MeasureSpec.AT_MOST で最大値を指定して計算し、計算結果を setMeasuredDimension で設定することで、最大値以上にならないようにしています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?