Home > Back-end >  How to use with to multiple params Kotlin?
How to use with to multiple params Kotlin?

Time:09-24

i have 4 views and i want to change their height and width to the same value, how can i reduce the amount of code here?

private fun changeSize() {
        with(binding) {
            with(leftBotIv) {
                layoutParams.height = height
                layoutParams.width = width
            }
            with(leftTopIv) {
                layoutParams.height = height
                layoutParams.width = width
            }
            with(rightBotIv) {
                layoutParams.height = height
                layoutParams.width = width
            }
            with(rightTopIv) {
                layoutParams.height = height
                layoutParams.width = width
            }
        }
    }

CodePudding user response:

Iterate them:

private fun changeSize() = with(binding) {
    arrayOf(leftBotIv, leftTopIv, rightBotIv, rightTopIv)
        .forEach {
            it.layoutParams.height = height
            it.layoutParams.width = width
        }
}
  • Related