How to add shadow to TextView on selection/focus

后端 未结 3 1115
情书的邮戳
情书的邮戳 2020-12-23 22:54

My question is how to add a shadow to text when TextView is selected or View that TextView is in gets selected. For example I have a CheckedTextView which changes background

3条回答
  •  时光说笑
    2020-12-23 23:05

    Yeah, I ran into the same problem, you can change the text color using a selector in xml, but not the shadowcolor. So in order to solve the problem, you might have to extend CheckedTextView or whatever View you need, and then override onDraw(Canvas canvas) according to the state of the View Thus, you need to use public void setShadowLayer (float radius, float dx, float dy, int color) defined in here

    for example:

    @Override
    protected void onDraw(Canvas canvas) {
        if(isPressed()){
            setShadowLayer(1, 0, 1, Color.RED);
        }else{
            if(isFocused()){
                setShadowLayer(1, 0, 1, Color.WHITE);
            }else{
                setShadowLayer(1, 0, 1, Color.BLACK);
            }
        }
        super.onDraw(canvas);
    }
    

    I hope that works

提交回复
热议问题