How to set transparent background for Image Button in code?

前端 未结 7 552
醉话见心
醉话见心 2020-12-25 09:03

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

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

How I can

相关标签:
7条回答
  • 2020-12-25 09:46

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

    0 讨论(0)
  • 2020-12-25 09:46

    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 
    
    0 讨论(0)
  • 2020-12-25 09:48

    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

    0 讨论(0)
  • 2020-12-25 09:54

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

        ImageButton btn=(ImageButton)findViewById(R.id.ImageButton01);
        btn.setBackgroundColor(Color.TRANSPARENT);
    
    0 讨论(0)
  • 2020-12-25 09:56

    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

    0 讨论(0)
  • 2020-12-25 10:07

    If you want to use android R class

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

    and don't forget to add support library to Gradle file

    compile 'com.android.support:support-v4:23.3.0'
    
    0 讨论(0)
提交回复
热议问题