代码之家  ›  专栏  ›  技术社区  ›  Namratha

如何拥有一个透明的ImageButton:Android

  •  455
  • Namratha  · 技术社区  · 16 年前
    <ImageButton android:id="@+id/previous"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/media_skip_backward"
    android:background="@drawable/transparent"></ImageButton>
    

    这是我试图得到一个透明的ImageButton,以便将这些按钮放在SurfaceView上。但是Eclipse在我将透明行包含在xml中时,就在项目中给了我一个错误。

    17 回复  |  直到 16 年前
        1
  •  1029
  •   Quintin Robinson    16 年前

    尝试使用null作为背景。。。

    android:background="@null"
    
        2
  •  336
  •   lory105    12 年前

    不要使用事务处理或空布局 因为这样按钮(或通用视图)在单击时将不再突出显示!!!

    我也有同样的问题 我从Android API中找到了正确的属性 解决问题。它可以应用于任何视图。

    在按钮规格中使用:

    android:background="?android:selectableItemBackground"
    
        3
  •  142
  •   Geykel    15 年前

    也可以使用透明颜色:

    android:background="@android:color/transparent"
    
        4
  •  113
  •   leoly    13 年前

    将背景设置为 "@null" 将使按钮在单击时无效。这将是一个更好的选择。

    style="?android:attr/borderlessButtonStyle"
    

    后来我发现

    android:background="?android:attr/selectableItemBackground"
    

    也是一个很好的解决方案。您可以以自己的样式继承此属性。

        5
  •  14
  •   Ronak Mehta    13 年前

    在运行时,可以使用以下代码

    btn.setBackgroundDrawable(null);
    
        6
  •  11
  •   Sam Stern    10 年前

    我认为公认的答案应该是: android:background="?attr/selectableItemBackground"

    这与@lory105的答案相同,但它使用支持库来实现最大的兼容性(即 android: 等效值仅适用于API>=11)

        7
  •  8
  •   Nishant Shah    16 年前

    删除此行:

    android:background="@drawable/transparent">
    

    在你的活动课上

    ImageButton btn = (ImageButton)findViewById(R.id.previous);
    btn.setAlpha(100);
    

    o表示透明,255表示不透明。

        8
  •  6
  •   nh7    6 年前

    如果需要单击动画,请不要使用null或transparent。更好:

    //Rectangular click animation
    android:background="?attr/selectableItemBackground"
    
    //Rounded click animation
    android:background="?attr/selectableItemBackgroundBorderless"
    
        9
  •  5
  •   Ajay Venugopal    11 年前

    android:background="#00000000"
    

    使用颜色代码#00000000使任何东西透明

        10
  •  2
  •   AskNilesh    8 年前

    使用 ImageView ... 它默认有透明的背景。。。

        11
  •  2
  •   Dika Hendy Evan    6 年前

    我已经在背景中添加了一些东西,所以,这个东西对我很有用:

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

    (Android Studio 3.4.1)

    编辑 :仅适用于android api级别21及以上。为了兼容性,请改用此选项

       android:background="@android:color/transparent"
    
        12
  •  1
  •   Pang Ajmal PraveeN    9 年前

    使用此选项:

    <ImageButton
     android:id="@+id/back"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@null"
     android:padding="10dp"
     android:src="@drawable/backbtn" />
    
        13
  •  1
  •   Murtuza Ali Khan Mohammed    9 年前

    在XML中将ImageButton的背景设置为@null

    <ImageButton android:id="@+id/previous"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/media_skip_backward"
    android:background="@null"></ImageButton>
    
        14
  •  1
  •   abir-cse    9 年前

    使用 “@null”

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/bkash"
        android:id="@+id/bid1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@null" />
    
        15
  •  1
  •   Nikolay Podolnyy    7 年前

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

    <ImageButton
        android:id="@+id/imageButton"
        android:src="@android:drawable/ic_menu_delete"
        android:background="@android:color/transparent"
    />
    
        16
  •  0
  •   Zar E Ahmer    11 年前

    通过编程将背景色设置为透明

     ImageButton btn=(ImageButton)findViewById(R.id.ImageButton01);
     btn.setBackgroundColor(Color.TRANSPARENT);
    
        17
  •  0
  •   Muhammed Refaat    10 年前

    image_button.setAlpha(0f) // to make it full transparent
    image_button.setAlpha(0.5f) // to make it half transparent
    image_button.setAlpha(0.6f) // to make it (40%) transparent
    image_button.setAlpha(1f) // to make it opaque
    
        18
  •  0
  •   Alireza Ghanbarinia    9 年前

    在XML集中 Background 属于任何颜色 White(#FFFFFF) Black(#000000) 如果你想要透明,只需在实际哈希代码前加80。

    #80000000   
    
        19
  •  -2
  •   Dave Clemmer manu    13 年前
    <ImageButton
        android:id="@+id/previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/media_skip_backward">
    </ImageButton>
    

    我用的是透明的 png 对于 ImageButton ,和 图片按钮

    推荐文章