Vertical (rotated) label in Android

后端 未结 10 1099
礼貌的吻别
礼貌的吻别 2020-11-22 08:04

I need 2 ways of showing vertical label in Android:

  1. Horizontal label turned 90 degrees counterclockwise (letters on the side)
  2. Horizontal label with l
10条回答
  •  醉梦人生
    2020-11-22 08:30

    Following Pointer Null's answer, I've been able to center the text horizontally by modifying the onDraw method this way:

    @Override
    protected void onDraw(Canvas canvas){
        TextPaint textPaint = getPaint();
        textPaint.setColor(getCurrentTextColor());
        textPaint.drawableState = getDrawableState();
        canvas.save();
        if(topDown){
            canvas.translate(getWidth()/2, 0);
            canvas.rotate(90);
        }else{
            TextView temp = new TextView(getContext());
            temp.setText(this.getText().toString());
            temp.setTypeface(this.getTypeface());
            temp.measure(0, 0);
            canvas.rotate(-90);
            int max = -1 * ((getWidth() - temp.getMeasuredHeight())/2);
            canvas.translate(canvas.getClipBounds().left, canvas.getClipBounds().top - max);
        }
        canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
        getLayout().draw(canvas);
        canvas.restore();
    }
    

    You might need to add a portion of the TextView measuredWidth to center a multilined text.

提交回复
热议问题