Programmatically set '?selectableItemBackground' on Android view

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

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



        
7条回答
  •  心在旅途
    2020-12-12 20:49

    I was looking for the same solution. I slightly changed this answer to make it more suitable for the asked question. Call the following code from your constructor.

    private void setClickableAnimation(Context context)
    {
        TypedValue outValue = new TypedValue();
        context.getTheme().resolveAttribute( 
            android.R.attr.selectableItemBackground, outValue, true);        
        setForeground(getDrawable(context, outValue.resourceId));
    }
    

提交回复
热议问题