【问题标题】:Transparent background in ImageButton with ripple effect?ImageButton中的透明背景具有波纹效果?
【发布时间】:2015-05-21 04:21:24
【问题描述】:

我想以编程方式移除 ImageButton 中的灰色背景。我尝试了很多删除它的方法,比如 -

imageButton.setBackgroundDrawable(null);

但是在实现它们时,我没有在触摸时对 ImageButton 产生涟漪效应。 (触摸时没有突出显示)。

有什么方法可以去除背景,但保留波纹效果或高光。

【问题讨论】:

标签: java android xml imagebutton


【解决方案1】:

如果android:background="?attr/selectableItemBackground" 有效,我相信这个答案应该可以解决您的问题:

https://stackoverflow.com/a/28087443/2534007

【讨论】:

  • 或者更好,android:background="?attr/selectableItemBackgroundBorderless"
  • 这会在某些设备上崩溃。
【解决方案2】:

创建您自己的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>

【讨论】:

  • &lt;ripple&gt; requires API level 21 (current min is 17) 所有好东西显然在几年后用户升级后会有用......
【解决方案3】:

想要有透明背景有波纹效果,背景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代码见谅,希望大家能看懂)

【讨论】:

    猜你喜欢
    • 2012-03-01
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多