如何在代码中设置图像按钮的透明背景?

时间:2011-01-17 04:25:48

标签: android background transparent imagebutton

我可以使用:

layout.xml中设置ImageButton背景透明
android:background="@android:color/transparent"

如何使用java代码完成同样的事情? 像ib.setBackgroundColor(???);

这样的东西

7 个答案:

答案 0 :(得分:132)

这很简单,只需要将背景颜色设置为透明

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

答案 1 :(得分:28)

在xml中执行

<ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButtonSettings"
        android:layout_gravity="right|bottom"
        android:src="@drawable/tabbar_settings_icon"
        android:background="@android:color/transparent"/>

答案 2 :(得分:10)

这应该有效 - imageButton.setBackgroundColor(android.R.color.transparent);

答案 3 :(得分:9)

请勿使用TRANSAPENT或NULL LAYOUT ,因为button(或通用视图)在点击时不再突出显示!!!

  

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

在按钮规格

中使用此功能
android:background="?android:selectableItemBackground"

这需要API 11

答案 4 :(得分:4)

试试这个

ImageButton imagetrans=(ImageButton)findViewById(R.id.ImagevieID);

imagetrans.setBackgroundColor(Color.TRANSPARENT);

OR

将此内容包含在res / layout

中的.xml文件中
android:background="@android:color/transparent 

答案 5 :(得分:3)

只需在图像按钮布局中使用它

android:background="@null"

使用

 android:background="@android:color/transparent 

 btn.setBackgroundColor(Color.TRANSPARENT);

没有提供完美的透明度

答案 6 :(得分:2)

如果你想使用android R class

textView.setBackgroundColor(ContextCompat.getColor(getActivity(), android.R.color.transparent));

并且不要忘记将支持库添加到Gradle文件

compile 'com.android.support:support-v4:23.3.0'
相关问题