以编程方式在 Android 视图上设置“?selectableItemBackground" [英] Programmatically set '?selectableItemBackground' on Android view

查看:53
本文介绍了以编程方式在 Android 视图上设置“?selectableItemBackground"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在xml中,我经常这样做是为了模拟onClick效果:

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

<android.support.v7.widget.CardView
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:foreground="?selectableItemBackground">

    ...

</android.support.v7.widget.CardView>

有没有办法在java中访问?selectableItemBackground?

Is there any way to access ?selectableItemBackground in java?

推荐答案

对于appcompat你可以使用,

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

这篇关于以编程方式在 Android 视图上设置“?selectableItemBackground"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆