篇首语:本文由小编为大家整理,主要介绍了向自定义CompoundButton添加涟漪效果相关的知识,希望对你有一定的参考价值。
我有以下自定义CompoundButton
:
public class CustomCompoundButton extends CompoundButton { public CustomCompoundButton(Context context) { this(context, null); } public CustomCompoundButton(Context context, AttributeSet attrSet) { super(context, attrSet); setup(); } private void setup() { setBackgroundResource(R.drawable.button_selector); setGravity(Gravity.CENTER); setClickable(true); }}
在将Button
添加到布局后,我从代码中设置了button.getLayoutParams().width = myWidth;button.getLayoutParams().height = myHeight;
的宽度和高度:
android="http://schemas.android.com/apk/res/android">
button_selector.xml:
button_checked.xml:
button_unchecked.xml:
Button
这可以按预期工作,selector
在未选中时为空圆圈,在选中时为圆圈。
问题是我无法在此行为之上添加涟漪效应。
我试图将ripple
包装在
标签中,如下所示:
shape
这种方法存在多个问题:
- 纹理完全覆盖了背景形状,它们不再可见(无论是否检查过)背景形状应该保持不变,我只想在点击按钮时添加涟漪效果(选中或取消选中)
- 涟漪效应的半径太大,它们相互重叠纹波半径应与我的按钮半径相同。
我不知道如何做这项工作,真的很感激任何建议。
答案
将每个ripple
包裹在自己的selector
而不是将整个ripple
包裹在this comment中将产生预期的效果。
有关示例,请参阅button_unchecked.xml
。
所以,
看起来像:
qazxswpoi
以上是关于向自定义CompoundButton添加涟漪效果的主要内容,如果未能解决你的问题,请参考以下文章