ImageButton doesn't highlight on click with Transparent background

前端 未结 4 1780
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 00:00

I\'ve set up an ImageButton to be transparent, so the icon matches the backgrond panel like the Android ActionBar. This looks fine as I want it to.

However, when the

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 00:29

    If you want to do it programmatically, here is one solution:

    Create a custom ImageButton class and Override drawableStateChange():

    public class CustomImageButton extends ImageButton {
    
        @Override
        protected void drawableStateChanged() {
            Log.d("Button", "isPressed: " + isPressed() );
            if( isPressed() ){
                setBackgroundResource( android.R.color.holo_blue_dark );
            }  else {
                setBackgroundResource( android.R.color.transparent );
            }
            super.drawableStateChanged();
    
        }
    
        public CustomImageButton( Context context ) {
            super( context );
        }
    
        public CustomImageButton( Context context, AttributeSet attrs ) {
            super( context, attrs );
        }
    
        public CustomImageButton( Context context, AttributeSet attrs, int defStyle ) {
            super( context, attrs, defStyle );
            // TODO Auto-generated constructor stub
        }
    
    
    
    }
    

提交回复
热议问题