How to set property “android:drawableTop” of a button at runtime

前端 未结 8 1014
名媛妹妹
名媛妹妹 2020-12-04 21:01

How to set property \"android:drawableTop\" of a button at runtime

相关标签:
8条回答
  • 2020-12-04 21:40

    If you are using Kotlin, you can use extension method to make things look elegant.

    fun TextView.setDrawableTop(iconId: Int) {
        val icon = this.context?.resources?.getDrawable(iconId)
        this.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null)
    }
    

    Then you can use it like this:

    // myTextView: TextView
    myTextView.setDrawableTop(R.drawable.ic_happy)
    
    0 讨论(0)
  • 2020-12-04 21:41

    Create an extension function like this and set top drawable like this

    tvAccepted.setTopDrawable(R.drawable.ic_preparing_order_active)
    
    fun TextView.setTopDrawable(icon: Int) {
        this.setCompoundDrawablesRelativeWithIntrinsicBounds(0,icon,0,0)
    }
    

    where

    setCompoundDrawablesRelativeWithIntrinsicBounds(left/start, top, right/end, bottom)

    0 讨论(0)
提交回复
热议问题