【问题标题】:Add selectableItemBackground to CardView将 selectableItemBackground 添​​加到 CardView
【发布时间】:2015-11-14 00:34:24
【问题描述】:

我想将我的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),但是当我按下卡片时前景不会改变。

【问题讨论】:

标签: android android-xml android-cardview


【解决方案1】:

问题是我将同一个 Drawable 用于多张卡片。

您必须为每张卡片检索一个新的 Drawable。

更多详情请见this answer

【讨论】:

    猜你喜欢
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    相关资源
    最近更新 更多