Programmatically set '?selectableItemBackground' on Android view

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

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



        
7条回答
  •  暖寄归人
    2020-12-12 20:39

    Based on @Wirling answer we can use foreground to set both the color and the ripple effect

    Note: Foreground requires android API level 23(M) and above:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {  
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        view.setForeground(getDrawable(getContext(), outValue.resourceId));
    }
    

提交回复
热议问题