Programmatically set '?selectableItemBackground' on Android view

后端 未结 7 1331
余生分开走
余生分开走 2020-12-12 20:25

In xml, I often do this to emulate onClick effect:



        
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-12 20:42

    For those who work with Kotlin, here are some extensions functions to add Ripple on Android View type :

    private fun View.addRipple() = with(TypedValue()) {
        context.theme.resolveAttribute(android.R.attr.selectableItemBackground, this, true)
        setBackgroundResource(resourceId)
    }
    
    private fun View.addCircleRipple() = with(TypedValue()) {
        context.theme.resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, this, true)
        setBackgroundResource(resourceId)
    }
    

提交回复
热议问题