如何删除 ImageButton 的标准背景图像?

ImageButton我想删除标准按钮的背景图像。在 http://developer.android.com中,人们说,一个人必须定义自己的背景图像或设置背景颜色是透明的。我试图设置一个黑色的背景,但它没有任何效果..。

55860 次浏览

No, it has to be transparent, not black. Try color: #00FFFFFF

You can use android:background="@null" for your ImageButton.

See also:
    Android Developers official guide on Buttons

use the following property in your in the xml of ImageButton:

android:background="@drawable/icon"

where icon is the name of the image kept in your drawable.

ImageButton.setBackgroundResource(0)

YourImageButton.setBackgroundColor(Color.TRANSPARENT);
myButton.setBackgroundResource(0);

Dot't use button.setBackgroundResource(0); on some devices you will get:

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

Better use button.setBackgroundColor(Color.TRANSPARENT);

The best choice is not set a transparent background to your ImageButton.

Give to your user a feedback when the button is tapped.

android:background="?attr/selectableItemBackgroundBorderless"

Use:

android:background="@null"

in your layout xml.

Using Kotlin, you can do this:

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))
})

In the latest android version, the background image doesn't remove even when you set android:background="@null".

Setting android:minHeight="0dp" works for me.