如何拥有一个透明的 ImageButton:Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3402787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to have a transparent ImageButton: Android
提问by Namratha
<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>
This is what I tried to get a transparent ImageButton so as to place those buttons on a SurfaceView. But Eclipse, gives me an error in the project as soon as I include the transparent line in xml.
这就是我试图获得透明 ImageButton 以便将这些按钮放置在 SurfaceView 上的方法。但是 Eclipse,只要我在 xml 中包含透明线,就会在项目中给我一个错误。
Please help.
请帮忙。
回答by Quintin Robinson
Try using null for the background ...
尝试使用 null 作为背景...
android:background="@null"
回答by lory105
DON'T USE A TRANSAPENT OR NULL LAYOUTbecause then the button (or the generic view) will no more highlight at click!!!
不要使用 TRANSAPENT 或 NULL 布局,因为这样按钮(或通用视图)将不再在单击时突出显示!!!
I had the same problem and finally I found the correct attribute from Android APIto solve the problem. It can apply to any view.
我遇到了同样的问题,最后我从 Android API 中找到了正确的属性来解决这个问题。它可以应用于任何视图。
Use this in the button specifications:
在按钮规格中使用它:
android:background="?android:selectableItemBackground"
回答by Geykel
You can also use a transparent color:
您还可以使用透明颜色:
android:background="@android:color/transparent"
回答by jiasli
Setting the background to "@null"
will make the button have no effect when clicked. This will be a better choice.
将背景设置为"@null"
将使按钮在单击时无效。这将是一个更好的选择。
style="?android:attr/borderlessButtonStyle"
Later I found that using
后来我发现使用
android:background="?android:attr/selectableItemBackground"
is also a good solution. And you can inherit this attribute in your own style.
也是一个很好的解决方案。您可以按照自己的风格继承此属性。
回答by Adem
in run time, you can use following code
在运行时,您可以使用以下代码
btn.setBackgroundDrawable(null);
回答by Sam Stern
I believe the accepted answer should be:
android:background="?attr/selectableItemBackground"
我相信接受的答案应该是:
android:background="?attr/selectableItemBackground"
This is the same as @lory105's answer but it uses the support library for maximum compatibility (the android:
equivalent is only available for API >= 11)
这与@lory105 的答案相同,但它使用支持库以实现最大兼容性(android:
等效项仅适用于 API >= 11)
回答by Nishant Shah
Remove this line :
删除这一行:
android:background="@drawable/transparent">
And in your activity class set
并在您的活动课程集中
ImageButton btn = (ImageButton)findViewById(R.id.previous);
btn.setAlpha(100);
You can set alpha level 0 to 255
您可以将 alpha 级别设置为 0 到 255
o means transparent and 255 means opaque.
o 表示透明,255 表示不透明。
回答by Ajay Venugopal
The best way is using the transparent color code
最好的方法是使用透明颜色代码
android:background="#00000000"
use the color code #00000000 for making any thing transparent
使用颜色代码 #00000000 使任何东西透明
回答by Daniel Gomez Rico
Use ImageView
... it have transparent background by default...
使用ImageView
......默认情况下它具有透明背景......
回答by Arsam
I was already adding something to the background so , This thing worked for me:
我已经在背景中添加了一些东西,所以这对我有用:
android:backgroundTint="@android:color/transparent"
(Android Studio 3.4.1)
(安卓工作室 3.4.1)
EDIT: only works on android api level 21 and above. for compatibility, use this instead
编辑:仅适用于 android api 级别 21 及以上。为了兼容性,请改用它
android:background="@android:color/transparent"