我想设置我的 CardView 的前景到 ?attr/selectableItemBackground
。
当我在 xml 中声明前景时它起作用了:
android:foreground="?attr/selectableItemBackground"
但我想以编程方式设置前景。这不起作用:
int[] attrs = new int[]{R.attr.color_a, R.attr.selectableItemBackground};
TypedArray ta = context.obtainStyledAttributes(attrs);
mColorA = ta.getColor(0, 0);
mSelectableItemBackground = ContextCompat.getDrawable(context, ta.getResourceId(1, 0));
ta.recycle();
...
cardView.setOnClickListener(onClickListener);
cardView.setClickable(true);
cardView.setForeground(mSelectableItemBackground);
但是,检索属性有效(mSelectableItemBackground 包含一个 RippleDrawable
),但是当我按下 Card 时前景不会改变。
最佳答案
关于android - 将 selectableItemBackground 添加到 CardView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33703844/