Place ImageView over Button android

后端 未结 9 1984
醉话见心
醉话见心 2020-11-30 06:35

I am trying to place an ImageView over a Button using RelativeLayout. Here is my xml:



        
9条回答
  •  半阙折子戏
    2020-11-30 07:00

    If you want to get an ImageView on top of a Button, and you are developing for Android API < 21 (for instance, KitKat = 19), the easiest way is to not use a Button at all and use 2 ImageView instead. Why would you want to do that? May be because you defined a drawable shape to make the button look "cooler", so you are already using android:background with that shape.

    Ex:

    Where @drawable/buttonshape.xml is:

    
    
    
    
    
    
    
    

    In that case, you should replace the Button with an ImageView, change android:background to android:src, and then, in the java code, you just add an OnClickListener as if it was a Button (both controls derive from View, and OnClickListener is a View event). Works like a charm!

提交回复
热议问题