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

请帮助。


当前回答

删除这一行:

android:background="@drawable/transparent">

在你的活动课集中

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

可以将alpha级别0设置为255

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

其他回答

如果你想在.xml文件中使用下面的代码:

android:background="@null"

这是一个编程的例子

yourButton.setBackgroundResource(0);

最好的方法是使用透明的颜色代码

android:background="#00000000"

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

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

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

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

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

(Android Studio 3.4.1)

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

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

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

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

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