<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就会在项目中出现错误。
请帮忙。
尝试使用null作为背景...
android:background="@null"
在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>
使用“@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" />
这是android:background="@android:color/transparent"
<ImageButton
android:id="@+id/imageButton"
android:src="@android:drawable/ic_menu_delete"
android:background="@android:color/transparent"
/>
这是以编程方式设置的背景颜色为透明
ImageButton btn=(ImageButton)findViewById(R.id.ImageButton01);
btn.setBackgroundColor(Color.TRANSPARENT);
以编程方式可以通过以下方式完成:
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
在您的XML集Background
属性为任何颜色White(#FFFFFF)
shade或Black(#000000)
shade.if你想透明度只是在实际的哈希代码之前放80。
#80000000
用这个:
<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" />
我已经在后台添加了一些东西,所以这件事对我有用:
android:backgroundTint="@android:color/transparent"
(Android Studio 3.4.1)
<ImageButton
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/media_skip_backward">
</ImageButton>
我使用透明png
为ImageButton
,而ImageButton
工作。
不要使用TRANSAPENT或NULL布局,因为按钮(或通用视图)将不会在点击时突出显示!
我遇到了同样的问题,最后我从Android API找到了正确的属性来解决问题。它可以应用于任何视图。
在按钮规格中使用它:
android:background="?android:selectableItemBackground"
您还可以使用透明色:
android:background="@android:color/transparent"
将背景设置为"@null"
将使按钮在单击时无效。这将是一个更好的选择。
style="?android:attr/borderlessButtonStyle"
后来我发现使用了
android:background="?android:attr/selectableItemBackground"
也是一个很好的解决方案。并且您可以以自己的样式继承此属性。
在运行时,您可以使用以下代码
btn.setBackgroundDrawable(null);
我相信接受的答案应该是:
android:background="?attr/selectableItemBackground"
这与@ lory105的答案相同,但它使用支持库以实现最大兼容性(android:
等效项仅适用于API> = 11)
删除此行:
android:background="@drawable/transparent">
并在您的活动类集中
ImageButton btn = (ImageButton)findViewById(R.id.previous);
btn.setAlpha(100);
您可以将alpha级别设置为0到255
o表示透明,255表示不透明。
最好的方法是使用透明色码
android:background="#00000000"
使用颜色代码#00000000使任何事物透明
使用ImageView
...默认情况下它具有透明背景......