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

请帮助。


当前回答

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

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

其他回答

<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工作正常。

背景设置为“@null”将使按钮在点击时无效。这将是更好的选择。

style="?android:attr/borderlessButtonStyle"

后来我发现这很有用

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

也是一个很好的解决方案。你可以用你自己的风格继承这个属性。

不要忘记添加填充,它有助于显示按钮效果。添加填充来调整背景。

android:background="?android:selectableItemBackground"
android:padding="5dp"

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

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

(Android Studio 3.4.1)

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

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

删除这一行:

android:background="@drawable/transparent">

在你的活动课集中

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

可以将alpha级别0设置为255

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