如何有一个透明的ImageButton:Android
<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,以便将这些button放在SurfaceView上。 但是,Eclipse,只要我在xml中包含透明线,就给我一个项目的错误。
请帮忙。
尝试使用null作为背景…
 android:background="@null" 
不要使用一个TRANSAPENT或NULL布局,因为那么button(或通用视图)将不再突出显示点击!
我有同样的问题,最后我从Android API中find正确的属性来解决问题。 它可以适用于任何视图。
在button规格中使用这个:
 android:background="?android:selectableItemBackground" 
你也可以使用透明的颜色:
 android:background="@android:color/transparent" 
 将背景设置为"@null"将使button在点击时不起作用。 这将是一个更好的select。 
 style="?android:attr/borderlessButtonStyle" 
后来我发现使用
 android:background="?android:attr/selectableItemBackground" 
也是一个很好的解决scheme。 你可以用你自己的风格inheritance这个属性。
在运行时,您可以使用以下代码
 btn.setBackgroundDrawable(null); 
删除这一行:
 android:background="@drawable/transparent"> 
并在你的活动课上设置
 ImageButton btn = (ImageButton)findViewById(R.id.previous); btn.setAlpha(100); 
您可以将alpha级别设置为0到255
o表示透明,255表示不透明。
 我相信接受的答案应该是: android:background="?attr/selectableItemBackground" 
 这与@ lory105的答案是一样的,但它使用支持库来实现最大的兼容性( android:等同于只适用于API> = 11) 
最好的方法是使用透明的颜色代码
 android:background="#00000000" 
使用颜色代码#00000000使任何事情透明
使用ImageView …它默认具有透明背景…
 只需在你的.java文件中使用这个代码: 
 ImageButton btn = (ImageButton) findViewById(R.id.ImageButton01); btn.setBackgroundColor(Color.TRANSPARENT); 
这是编程设置为透明的背景颜色
  ImageButton btn=(ImageButton)findViewById(R.id.ImageButton01); btn.setBackgroundColor(Color.TRANSPARENT); 
以编程方式可以通过以下方式完成:
 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 
 在你的XML中设置Background属性为任何颜色的White(#FFFFFF)阴影或者Black(#000000) shade.if你想要透明度只是把80放在实际的散列码之前。 
 #80000000 
用这个:
 <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" /> 
将ImageButton的背景设置为XML中的@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> 
使用“@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" /> 
 <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工作。