Android 如何卸下ImageButton';标准背景图像是什么?

Android 如何卸下ImageButton';标准背景图像是什么?,android,imagebutton,Android,Imagebutton,在ImageButton中,我想删除标准按钮背景图像。据说,必须定义自己的背景图像或将背景颜色设置为透明。我试图设置黑色背景,但没有任何效果…不,它必须是透明的,而不是黑色的。试试颜色:#00FFFFFF您可以使用android:background=“@null”作为您的应用程序 另请参见: 在xml中的ImageButton中使用以下属性: android:background="@drawable/icon" 其中icon是保存在绘图表中的图像的名称。ImageButton.setBac

ImageButton
中,我想删除标准按钮背景图像。据说,必须定义自己的背景图像或将背景颜色设置为透明。我试图设置黑色背景,但没有任何效果…

不,它必须是透明的,而不是黑色的。试试颜色:#00FFFFFF

您可以使用
android:background=“@null”
作为您的应用程序

另请参见:

在xml中的ImageButton中使用以下属性:

android:background="@drawable/icon"

其中icon是保存在绘图表中的图像的名称。

ImageButton.setBackgroundResource(0)
请勿使用
按钮。setBackgroundResource(0)
; 在某些设备上,您将获得:

android.content.res.Resources$NotFoundException:资源ID#0x0


最好使用
按钮.setBackgroundColor(Color.TRANSPARENT)

最好的选择不是为
图像按钮设置透明背景

点击按钮时向用户提供反馈

android:background="?attr/selectableItemBackgroundBorderless"
使用:


在布局xml中。

使用Kotlin,可以执行以下操作:

val myImageButton = ImageButton(context).apply({
    background = null

    // and if you need to add drawable, simply use:

    setImageDrawable(ContextCompat.getDrawable(context, 
                         R.drawable.ic_save_black_24px))
})

杰出的这是以编程方式执行Mudassir解决方案的解决方案。非常感谢。这是挫折资源,不是挫折资源。如下使用:((ImageButton)findViewById(R.id.my_按钮)).setBackgroundResource(0);这是一个比设置
src
和清空背景更干净的解决方案。极好的解决方案。这个答案应该标记为正确。
android:background="?attr/selectableItemBackgroundBorderless"
android:background="@null"
val myImageButton = ImageButton(context).apply({
    background = null

    // and if you need to add drawable, simply use:

    setImageDrawable(ContextCompat.getDrawable(context, 
                         R.drawable.ic_save_black_24px))
})