<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会在项目中给我一个错误。

请帮助。


当前回答

如果你需要一个点击动画,不要使用空或透明。好:

//Rectangular click animation
android:background="?attr/selectableItemBackground"

//Rounded click animation
android:background="?attr/selectableItemBackgroundBorderless"

其他回答

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

不要使用透明或空布局,因为这样按钮(或通用视图)在点击时将不再突出显示!!

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

在按钮规范中使用这个:

android:background="?android:selectableItemBackground"

删除这一行:

android:background="@drawable/transparent">

在你的活动课集中

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

可以将alpha级别0设置为255

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

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

btn.setBackgroundDrawable(null);

在编程上,它可以通过:

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