In ImageButton
I want to remove the standard button background image. In http://developer.android.com it is said, that one must define his\her own background image or set the background color to be transparent. I tried to set a black background, but it didn't make any effect...
You can use android:background="@null"
for your ImageButton.
ImageButton.setBackgroundResource(0)
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))
})
来源:https://stackoverflow.com/questions/5457138/how-to-remove-imagebuttons-standard-background-image