2012-03-02 27 views
20

我有一个ImageButton,我想删除围绕该图像的丑陋(恕我直言)背景。我可以添加一个ImageView,但是它们很难在布局中完美设置,如图中所示的灰色。 [重力“中心”不会让它走到中间,只是垂直居中。)任何方式从ImageButtons删除灰色背景?

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

enter image description here

+2

你是如何将图像添加到图像按钮? android:src或android:background – 2012-03-02 02:52:07

+0

我只是将它拖入图形编辑器,[this](http://i.imgur.com/d2VTW.png)出现了,让我选择我的图片。但是每个ImageButton都有它周围的灰色渐变背景。 – Cole 2012-03-02 02:56:55

回答

27

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

ImageButton imageButton = new ImageButton(this); 
imageButton.setBackgroundDrawable(null); 
0

如果设置的

android:background 

代替

android:src 

它应该覆盖的灰色背景

18

默认背景是不是透明的。

所以,只需添加透明颜色“#00000000”作为背景,然后你就可以解决它。

p.s. #00000000是透明色

<ImageButton 
android:id="@+id/imageButton1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/icon" 
android:background="#00000000" 
/> 
+0

谷歌/ Android应该真的告诉你默认背景是不透明的,节省一个小时看谷歌和stackoverflow – 2018-02-11 14:42:58

0

是的。如果拖动通常会要求ImageSource的ImageButton。但是ImageSource(android:src)不会去除灰色背景。这是一个应该在背景之上的图像:

而不是在layout XML中尝试android:background。

0

请使用绘图9补丁格式的图像b'use它是由SDK提供

1. From a terminal, launch the draw9patch application from your SDK /tools directory. 
2. Drag your PNG image into the Draw 9-patch window (or File > Open 9-patch... to locate the file). Your workspace will now open. 

左侧窗格是您的绘图区域,您可以在其中编辑可伸缩修补程序和内容区域的线条。右窗格是预览区域,您可以在其中拉伸时预览图形。

3. Click within the 1-pixel perimeter to draw the lines that define the stretchable patches and (optional) content area. Right-click (or hold Shift and click, on Mac) to erase previously drawn lines. 

4. When done, select File > Save 9-patch... 

您的图像将以.9.png文件名保存。

3

只使用android:background="@null"在XML

+0

干杯真的帮助! – 2018-02-11 14:41:48

0

您可以使用

android:background="@drawable/icon"

代替

android:src="@drawable/icon"

这不会给任何灰色背景的ImageButton。

0

使用这样

android:background="@android:color/transparent" 

android:src="@drawable/icon" 
0

我有同样的问题,但现在已经解决了。如果您正在使用“机器人:背景=” @空”它会导致完全没有背景,没有按钮被按下动画会出现用户

刚加入这行代码,以使图像按钮。 。成为点击时闪烁在此提醒图像按钮被点击的用户的正确方法

 **"android:background="?android:selectableItemBackground"** 
2

你可以把方盒子,只是改变它的颜色是这样的:

android:backgroundTint="@color/colorPrimary" 

作品精美的