【问题标题】:ImageButton in Android with transparent background [duplicate]Android中具有透明背景的ImageButton [重复]
【发布时间】:2012-03-01 01:03:39
【问题描述】:

我已经按照这篇文章在 android 中制作 ImageButton

android image button

图像出现在按钮上,但它有一些背景,我的图像是 png 图像,我希望按钮是透明背景

请大家帮忙

【问题讨论】:

    标签: android imagebutton


    【解决方案1】:

    将此行添加到您的 ImageButton xml 布局中。

    android:background="@null"
    

    【讨论】:

    • 哇,这么简单……谢谢!
    • 但是我在背景属性本身中设置了图像!
    • 如果你想要一个透明背景的图像,你可以在 xml 布局中这样做:android:background="@null" android:src="@drawable/myimage"
    【解决方案2】:

    这是一个对我有用的替代解决方案:

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

    正如 Sunny 所说,将其添加到 ImageButtons 的 XML 布局中

    【讨论】:

      【解决方案3】:

      在你的代码中使用这个:

      ImageButton btn = new ImageButton(this);
      btn.setImageResource(R.drawable.btn_close);
      btn.setBackgroundResource(0);
      

      【讨论】:

      • 这对我有用,但另一种有效的方法是:btn.setBackgroundColor(0x00000000);
      【解决方案4】:

      我不知道是否有任何方法可以做到这一点,但是您可以绘制完全透明的 .png 文件并使用方法 setBackGroundResource() 或在 XML 中使用:android:background

      【讨论】:

        【解决方案5】:

        通过这种方式可以给ImageButton设置透明背景:

        <ImageButton android:id="@+id/imagebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/<imageresourcename>"
        android:background="@null"></ImageButton>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-02-21
          • 1970-01-01
          • 1970-01-01
          • 2012-08-21
          • 1970-01-01
          • 1970-01-01
          • 2012-12-22
          • 2021-08-24
          相关资源
          最近更新 更多