Programmatically set '?selectableItemBackground' on Android view

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

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



        
7条回答
  •  再見小時候
    2020-12-12 20:43

    From a fragment:

    TypedValue outValue = new TypedValue();
    requireContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
    cardView.setBackgroundResource(outValue.resourceId);
    

    From an adapter declaring in its constructor the context or the same fragment:

    TypedValue outValue = new TypedValue();
    fragment.requireContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
    holder.cardView.setBackgroundResource(outValue.resourceId);
    

提交回复
热议问题