2015-11-14 38 views
2

我想將我的CardView的前景設置爲?attr/selectableItemBackground將SelectableItemBackground添加到CardView

它工作時,我宣佈在前景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),但是當我按下插卡的前景不會改變。

+0

我想我只是在這裏找到了一個可能的解決方案:http://stackoverflow.com/q/27474279/4752807 ..我也在多個卡上使用相同的實例..我必須調查,然後將更新此問題。 – Auroratic

+0

你見過這個答案嗎? http://stackoverflow.com/a/30192562/4409409 –

回答

1

問題是我使用了與多張卡片相同的Drawable。

您必須爲每張卡取回新的Drawable。

更多詳細信息在this answer