android代码设置点击涟漪,android – 为自定义CompoundButton添加涟漪效果

android代码设置点击涟漪,android – 为自定义CompoundButton添加涟漪效果我有以下自定义CompoundButton:publicclassCustomCompoundButtonextendsCompoundButton{publicCustomCompoundButton(Contextcontext){this(context,null);}publicCustomCompoundButton(Contextcontext,Attribut…

大家好,又见面了,我是你们的朋友全栈君。

我有以下自定义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;

button_selector.xml:

android:drawable=”@drawable/button_checked”

android:state_checked=”true” />

android:drawable=”@drawable/button_unchecked”

android:state_checked=”false” />

button_checked.xml:

android:shape=”oval”>

button_unchecked.xml:

android:shape=”oval”>

android:width=”2dp”

android:color=”?colorAccent” />

这按预期工作,未选中时按钮为空圆圈,选中时为实心圆圈.

问题是我无法在此行为之上添加涟漪效应.

我试图将选择器包装在ripple标签中,如下所示:

android:color=”#ffffff”>

android:drawable=”@drawable/button_checked”

android:state_checked=”true” />

android:drawable=”@drawable/button_unchecked”

android:state_checked=”false” />

这种方法存在多个问题:

>背景形状被涟漪完全覆盖,它们不再可见(无论它们是否被检查)

背景形状应该保持不变,我只想在点击按钮时添加涟漪效果(选中或取消选中)

>涟漪效应的半径太大,它们相互重叠

纹波半径应与我的按钮半径相同.

我不知道如何做这项工作,真的很感激任何建议.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/137681.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号