有什么方法可以从ImageButtons中删除灰色背景?

Col*_*ole 24 eclipse android

我有一个ImageButton,我想删除围绕图像的丑陋(恕我直言)背景.我可以添加一个ImageView,但是它们很难在布局中完美地设置,就像图中的灰色一样.[重力"中心"不会让它向中间移动,只是垂直居中.)

那么有什么方法可以删除它吗?

在此输入图像描述

Ngu*_*inh 35

只需使用android:background="#0000" (#0000与#00000000相同)或

ImageButton imageButton = new ImageButton(this);
imageButton.setBackgroundDrawable(null);
Run Code Online (Sandbox Code Playgroud)

  • `setBackgroundDrawable` 从 API 16: Android 4.1 (Jelly Bean) 开始被弃用。改用`setBackground` (2认同)

don*_*221 23

默认背景不透明.

因此,只需添加透明色"#00000000"作为背景,然后就可以解决它.

ps#00000000是透明色

<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon" 
android:background="#00000000"
/>
Run Code Online (Sandbox Code Playgroud)


小智 5

我遇到了同样的问题,但现在已经解决了。如果您正在使用"android:background="@null"它将导致根本没有背景,并且不会向用户显示按钮按下的动画。

只需添加这行代码即可使图像按钮在单击时闪烁。这是提醒用户图像按钮已被单击的正确方法。

 "android:background="?android:selectableItemBackground" 
Run Code Online (Sandbox Code Playgroud)


Dav*_*d A 5

You can keep the square box and just change its color like this:

android:backgroundTint="@color/colorPrimary"
Run Code Online (Sandbox Code Playgroud)

works beautifully