如何拥有透明的ImageButton:Android

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/media_skip_backward"

android:background="@drawable/transparent">

这就是我试图获得透明的ImageButton以便将这些按钮放在SurfaceView上。 但是,只要我在xml中包含透明线,Eclipse就会在项目中出现错误。

请帮忙。

17个解决方案

907 votes

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

android:background="@null"

Quintin Robinson answered 2019-03-29T19:14:41Z

258 votes

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

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

在按钮规格中使用它:

android:background="?android:selectableItemBackground"

lory105 answered 2019-03-29T19:15:18Z

132 votes

您还可以使用透明色:

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

Geykel answered 2019-03-29T19:15:41Z

104 votes

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

style="?android:attr/borderlessButtonStyle"

后来我发现使用了

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

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

Eliko answered 2019-03-29T19:16:18Z

14 votes

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

btn.setBackgroundDrawable(null);

Adem answered 2019-03-29T19:16:42Z

10 votes

我相信接受的答案应该是:android:

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

Sam Stern answered 2019-03-29T19:17:14Z

7 votes

删除此行:

android:background="@drawable/transparent">

并在您的活动类集中

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

btn.setAlpha(100);

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

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

Nishant Shah answered 2019-03-29T19:18:08Z

5 votes

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

android:background="#00000000"

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

Ajay Venugopal answered 2019-03-29T19:18:39Z

1 votes

只需在.java文件中使用此代码:

ImageButton btn = (ImageButton) findViewById(R.id.ImageButton01);

btn.setBackgroundColor(Color.TRANSPARENT);

emi raza siddique answered 2019-03-29T19:19:08Z

1 votes

在XML中将ImageButton的背景设置为@null

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/media_skip_backward"

android:background="@null">

Murtuza Khan answered 2019-03-29T19:19:36Z

1 votes

使用“@null”。 它对我有用。

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" />

abir99 answered 2019-03-29T19:20:15Z

1 votes

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

Caipivara answered 2019-03-29T19:20:41Z

0 votes

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

ImageButton btn=(ImageButton)findViewById(R.id.ImageButton01);

btn.setBackgroundColor(Color.TRANSPARENT);

Xar E Ahmer answered 2019-03-29T19:21:06Z

0 votes

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

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

Muhammed Refaat answered 2019-03-29T19:21:30Z

0 votes

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

#80000000

Alireza Ghanbarinia answered 2019-03-29T19:21:55Z

0 votes

用这个:

android:id="@+id/back"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@null"

android:padding="10dp"

android:src="@drawable/backbtn" />

Akshay Paliwal answered 2019-03-29T19:22:15Z

-2 votes

android:id="@+id/previous"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/media_skip_backward">

我使用透明png作为ImageButton,而ImageButton工作。

Kiyoshi answered 2019-03-29T19:22:41Z

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐