【问题标题】:Any way to remove the grey background from ImageButtons?有什么方法可以从 ImageButtons 中删除灰色背景?
【发布时间】:2012-03-20 14:26:17
【问题描述】:

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

那么有什么办法可以去掉呢?

【问题讨论】:

  • 如何将图像添加到图像按钮? android:src 或 android:background
  • 我只是把它拖到图形编辑器中,然后this 出现了,让我选择我的图片。但是每个 ImageButton 周围都有灰色渐变背景。

标签: android eclipse


【解决方案1】:

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

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

【讨论】:

  • setBackgroundDrawable 自 API 16 起已弃用:Android 4.1 (Jelly Bean)。请改用setBackground
  • setBackground(null)
【解决方案2】:

默认背景不透明。

所以,只需将透明色“#00000000”添加为背景,即可解决。

附言#00000000 为透明色

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

【讨论】:

  • Google/Android 应该告诉你默认背景不是透明的,可以节省一个小时查看 google 和 stackoverflow
【解决方案3】:

您可以保留方形框并像这样更改其颜色:

android:backgroundTint="@color/colorPrimary"

效果很好

【讨论】:

    【解决方案4】:

    只需在您的 xml 中使用android:background="@null"

    【讨论】:

    • 干杯真的很有帮助!
    【解决方案5】:

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

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

     "android:background="?android:selectableItemBackground" 
    

    【讨论】:

      【解决方案6】:

      如果你设置了

      android:background 
      

      而不是

      android:src 
      

      它应该覆盖灰色背景

      【讨论】:

        【解决方案7】:

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

        在 XML 布局中尝试使用 android:background。

        【讨论】:

          【解决方案8】:

          请使用 SDK 提供的 Draw 9-patch 格式的图像

          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 文件名保存。

          【讨论】:

            【解决方案9】:

            这样使用

            android:background="@android:color/transparent"
            
            android:src="@drawable/icon"
            

            【讨论】:

              【解决方案10】:

              所有提议的解决方案都删除了波纹动画(点击时)

              将背景色调模式设置为“添加”将删除背景并保留波纹动画:

              android:backgroundTintMode="add"
              

              【讨论】:

                【解决方案11】:

                如果使用 Xamarin Forms,可以在 xaml 视图中这样做。将背景设置为透明。

                <ImageButton BackgroundColor="Transparent"
                

                【讨论】:

                  猜你喜欢
                  • 2016-01-24
                  • 2014-12-31
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-12-29
                  • 1970-01-01
                  • 2021-10-28
                  • 2012-09-27
                  • 2023-02-20
                  相关资源
                  最近更新 更多