Change linear layout top margin programmatically android

后端 未结 7 1084
难免孤独
难免孤独 2021-02-01 02:31

i have two linear layouts in one frame layout.



        
7条回答
  •  不思量自难忘°
    2021-02-01 03:19

    Kotlin solution:

    fun LinearLayout.setMargins(left: Int? = null, top: Int? = null, right: Int? = null, bottom: Int? = null) {
        val params: LinearLayout.LayoutParams = this.layoutParams as LinearLayout.LayoutParams
    
        val left = left?.let { it } ?: params.leftMargin
        val top = top?.let { it } ?: params.topMargin
        val right = right?.let { it } ?: params.rightMargin
        val bottom = bottom?.let { it } ?: params.bottomMargin
    
        params.setMargins(left, top, right, bottom)
        this.requestLayout()
    }
    

    then you just need to:

    layoutbtnlinear_aboutme.setMargins(top = 10)
    

提交回复
热议问题