【发布时间】:2015-05-21 04:21:24
【问题描述】:
我想以编程方式移除 ImageButton 中的灰色背景。我尝试了很多删除它的方法,比如 -
imageButton.setBackgroundDrawable(null);
但是在实现它们时,我没有在触摸时对 ImageButton 产生涟漪效应。 (触摸时没有突出显示)。
有什么方法可以去除背景,但保留波纹效果或高光。
【问题讨论】:
标签: java android xml imagebutton
我想以编程方式移除 ImageButton 中的灰色背景。我尝试了很多删除它的方法,比如 -
imageButton.setBackgroundDrawable(null);
但是在实现它们时,我没有在触摸时对 ImageButton 产生涟漪效应。 (触摸时没有突出显示)。
有什么方法可以去除背景,但保留波纹效果或高光。
【问题讨论】:
标签: java android xml imagebutton
如果android:background="?attr/selectableItemBackground" 有效,我相信这个答案应该可以解决您的问题:
【讨论】:
android:background="?attr/selectableItemBackgroundBorderless"
创建您自己的RippleDrawable,如果您要使用透明背景,则需要为波纹使用遮罩。
<!-- A red ripple masked against an opaque rectangle. -->
<ripple android:color="#ffff0000">
<item android:id="@android:id/mask"
android:drawable="@android:color/white" />
</ripple>
【讨论】:
<ripple> requires API level 21 (current min is 17) 所有好东西显然在几年后用户升级后会有用......
想要有透明背景有波纹效果,背景Drawable需要是rippleDrawable,可以是透明的。像这样以编程方式设置它。
var mask = new android.graphics.drawable.GradientDrawable();
mask.setShape(android.graphics.drawable.GradientDrawable.RECTANGLE);
mask.setColor(0xFF000000); // the color is irrelevant here, only the alpha
mask.setCornerRadius(5); // you can have a rounded corner for the effect
var rippleColorLst = android.content.res.ColorStateList.valueOf(
android.graphics.Color.argb(255,50,150,255) // set the color of the ripple effect
);
// aaaand
var ripple = new android.graphics.drawable.RippleDrawable(rippleColorLst,null,mask);
yourImageButton.setBackground(ripple);
(JavaScript/NativeScript代码见谅,希望大家能看懂)
【讨论】: