How to remove ImageButton's standard background image?

孤街浪徒 提交于 2019-11-28 17:21:34

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

ImageButton.setBackgroundResource(0)

Filipe Brito

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"

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

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.

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

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

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))
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!