android button selector

前端 未结 6 619
梦毁少年i
梦毁少年i 2020-11-22 04:19

This is a button selector such that when normal it appears red, when pressed it appears grey.

I would like to ask how could the code be further directly modified suc

6条回答
  •  遥遥无期
    2020-11-22 05:11

    You can't achieve text size change with a state list drawable. To change text color and text size do this:

    Text color

    To change the text color, you can create color state list resource. It will be a separate resource located in res/color/ directory. In layout xml you have to set it as the value for android:textColor attribute. The color selector will then contain something like this:

    
    
        
        
    
    

    Text size

    You can't change the size of the text simply with resources. There's no "dimen selector". You have to do it in code. And there is no straightforward solution.

    Probably the easiest solution might be utilizing View.onTouchListener() and handle the up and down events accordingly. Use something like this:

    view.setOnTouchListener(new OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    // change text size to the "pressed value"
                    return true;
                case MotionEvent.ACTION_UP:
                    // change text size to the "normal value"
                    return true;
                default:
                    return false;
                }
            }
    });
    

    A different solution might be to extend the view and override the setPressed(Boolean) method. The method is internally called when the change of the pressed state happens. Then change the size of the text accordingly in the method call (don't forget to call the super).

提交回复
热议问题