如何拥有透明的ImageButton:Android

问题描述 投票:463回答:18
<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 transparent imagebutton surfaceview
18个回答
951
投票

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

android:background="@null"

1
投票

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

1
投票

使用“@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" />

1
投票

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

<ImageButton
    android:id="@+id/imageButton"
    android:src="@android:drawable/ic_menu_delete"
    android:background="@android:color/transparent"
/>

0
投票

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

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

0
投票

以编程方式可以通过以下方式完成:

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

0
投票

在您的XML集Background属性为任何颜色White(#FFFFFF) shade或Black(#000000) shade.if你想透明度只是在实际的哈希代码之前放80。

#80000000   

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
投票

我已经在后台添加了一些东西,所以这件事对我有用:

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

(Android Studio 3.4.1)


-2
投票
<ImageButton
    android:id="@+id/previous"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/media_skip_backward">
</ImageButton>

我使用透明pngImageButton,而ImageButton工作。


275
投票

不要使用TRANSAPENT或NULL布局,因为按钮(或通用视图)将不会在点击时突出显示!

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

在按钮规格中使用它:

android:background="?android:selectableItemBackground"

136
投票

您还可以使用透明色:

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

108
投票

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

style="?android:attr/borderlessButtonStyle"

后来我发现使用了

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

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


14
投票

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

btn.setBackgroundDrawable(null);

11
投票

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

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


8
投票

删除此行:

android:background="@drawable/transparent">

并在您的活动类集中

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

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

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


5
投票

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

android:background="#00000000"

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


2
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.