Android 如何以编程方式设置LinearLayout上的涟漪效应?
我想将背景Android 如何以编程方式设置LinearLayout上的涟漪效应?,android,background,android-linearlayout,clickable,Android,Background,Android Linearlayout,Clickable,我想将背景android.R.attr.selectableItemBackground设置为LinearLayout。使用XML时没有问题(它可以工作) 。。。它不起作用,事实上,我在第二行得到了一个NotFoundException。 所以在我尝试了这个变体之后,我认为资源是一种颜色 llMiner.setClickable(true); llMiner.setBackgroundColor(android.R.attr.selectableItemBackground); 这一个不会启动
android.R.attr.selectableItemBackground
设置为LinearLayout
。使用XML时没有问题(它可以工作)
。。。它不起作用,事实上,我在第二行得到了一个NotFoundException
。
所以在我尝试了这个变体之后,我认为资源是一种颜色
llMiner.setClickable(true);
llMiner.setBackgroundColor(android.R.attr.selectableItemBackground);
这一个不会启动异常,但是。。。不起作用(按下时背景不会改变,但按下时状态会发生改变)。。。有什么建议吗?你可以用这种方式
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// If we're running on Honeycomb or newer, then we can use the Theme's
// selectableItemBackground to ensure that the View has a pressed state
TypedValue outValue = new TypedValue();
this.getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
textView.setBackgroundResource(outValue.resourceId);
}
您是否正在更改活动的onClick方法中的颜色..???然后我认为您必须重写onClick方法才能完成功能。我正在尝试做同样的事情,但迄今为止没有成功。如果您找到了解决方案,请让我知道,或者如果有人知道这是否真的可以做到,请给出反馈。谢谢!对于寻找支持库对应项的任何人来说,它是android.support.design.R.attr.selectableItemBackground。我之前的评论现在似乎不起作用(或者可能从未起作用)。现在有效的是
R.attr.selectableItemBackground
。不适用于我,白色,如果您想保留主题颜色,请使用R.attr.selectableItemBackground而不是android.R.attr.selectableItemBackground使用R.attr.selectableItemBackground。selectableItemBackground
将使用AppCompat的属性(如果您包括该属性)默认情况下,对于预棒棒糖,会返回淡入/淡出,而不是ripple。请注意,R
是您的packagename.R
。请投票选择有效答案,它确实很有帮助,谢谢
llMiner.setClickable(true);
llMiner.setBackgroundColor(android.R.attr.selectableItemBackground);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// If we're running on Honeycomb or newer, then we can use the Theme's
// selectableItemBackground to ensure that the View has a pressed state
TypedValue outValue = new TypedValue();
this.getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
textView.setBackgroundResource(outValue.resourceId);
}