首页 文章

如何拥有透明的ImageButton:Android

提问于
浏览
446
<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上 . 但是,只要在xml中包含透明线,Eclipse就会在项目中出错 .

请帮忙 .

17 回答

  • 0

    Programmatically 可以通过以下方式完成:

    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
    
  • 104

    使用 ImageView ...默认情况下它具有透明背景...

  • 132

    您还可以使用透明色:

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

    这是以编程方式设置的背景颜色为透明

    ImageButton btn=(ImageButton)findViewById(R.id.ImageButton01);
     btn.setBackgroundColor(Color.TRANSPARENT);
    
  • -2

    在您的XML中设置 Background 属性为任何颜色 White(#FFFFFF) shade或 Black(#000000) shade.if您希望透明度在实际哈希代码之前放置80 .

    #80000000
    
  • 10

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

    android:background="@null"
    
  • 0

    The best way is using the transparent color code

    android:background="#00000000"
    

    使用颜色代码#00000000使任何事物透明

  • 14

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

    style="?android:attr/borderlessButtonStyle"
    

    后来我发现使用了

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

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

  • 259

    我相信接受的答案应该是: android:background="?attr/selectableItemBackground"

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

  • 907

    在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>
    
  • 0

    用这个:

    <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" />
    
  • 0

    使用 "@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" />
    
  • 7
    <ImageButton
        android:id="@+id/previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/media_skip_backward">
    </ImageButton>
    

    我使用 ImageButton 透明 png ,而 ImageButton 工作 .

  • 5

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

    btn.setBackgroundDrawable(null);
    
  • 1

    删除此行:

    android:background="@drawable/transparent">
    

    并在您的活动类集中

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

    您可以将alpha级别设置为0到255

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

  • 1

    DON'T USE A TRANSAPENT OR NULL LAYOUT 因为按钮(或通用视图)在点击时不再突出显示!!!

    我遇到了同样的问题,最后我从Android API找到了正确的属性来解决问题 . 它可以应用于任何视图 .

    在按钮规格中使用它:

    android:background="?android:selectableItemBackground"
    
  • 1

    只需在 .java 文件中使用此代码:

    ImageButton btn = (ImageButton) findViewById(R.id.ImageButton01);
    btn.setBackgroundColor(Color.TRANSPARENT);
    

相关问题