How to set transparent background for Image Button

2019-02-01 20:04发布

I can set ImageButton background transparent in layout.xml using:

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

How I can acomplish same thing using java code? Something like ib.setBackgroundColor(???);

7条回答
狗以群分
2楼-- · 2019-02-01 20:32

This should work - imageButton.setBackgroundColor(android.R.color.transparent);

查看更多
forever°为你锁心
3楼-- · 2019-02-01 20:33

Try like this

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

imagetrans.setBackgroundColor(Color.TRANSPARENT);

OR

include this in your .xml file in res/layout

android:background="@android:color/transparent 
查看更多
地球回转人心会变
4楼-- · 2019-02-01 20:34

simply use this in your imagebutton layout

android:background="@null"

using

 android:background="@android:color/transparent 

or

 btn.setBackgroundColor(Color.TRANSPARENT);

doesn't give perfect transparency

查看更多
我命由我不由天
5楼-- · 2019-02-01 20:37

This is the simple only you have to set background color as transparent

    ImageButton btn=(ImageButton)findViewById(R.id.ImageButton01);
    btn.setBackgroundColor(Color.TRANSPARENT);
查看更多
一夜七次
6楼-- · 2019-02-01 20:39

DON'T USE A TRANSAPENT OR NULL LAYOUT because then the button (or the generic view) will no more highlight at click!!!

I had the same problem and finally I found the correct attribute from Android API to solve the problem. It can apply to any view

Use this in the button specifications

android:background="?android:selectableItemBackground"

This requires API 11

查看更多
Anthone
7楼-- · 2019-02-01 20:53

Do it in your 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"/>
查看更多
登录 后发表回答