Rotation to TextView (vertical characters)

Asked 1 years ago, Updated 1 years ago, 132 views

I gave -90.0 to rotation to mark the text vertically, but there was a problem here, so I'm asking you a question ㅠ<

I gave it -90.0 in this state.

It is true that we are trying to go back like that and realize what we see, but the size of the Linear Layout surrounding that text view does not change

When I set the width of Linear Layout as wrap_content in picture 1, it was fixed to the size before rotation, so it remained in that size even after rotation, and there is space on both sides of the text view like in picture 2. I didn't want that "T" So I made Linear Layout's width equal to the text size, and before it was rotated, the text in the text view was rotated in two lines, and it remained in two lines even after rotation.

How do I make sure there are no spaces on both sides of the text view in picture 2?

android textview

2022-09-22 15:41

1 Answers

I think we can inherit TextView and turn it around.

package com.example.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomTextView extends TextView {

    public CustomTextView(Context context, AttributeSet attrs){
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    @Override
    protected void onDraw(Canvas canvas){
        TextPaint textPaint = getPaint();
        textPaint.setColor(getCurrentTextColor());
        textPaint.drawableState = getDrawableState();

        canvas.save();

        canvas.translate(getWidth(), 0);
        canvas.rotate(90); //Set desired orientation

        canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());

        getLayout().draw(canvas);
        canvas.restore();
    }
}

And in xml

    <com.example.myapplication.CustomTextView
        android:id="@+id/txt_123"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="abcdefgdddddd"
        />

You can name it like this.


2022-09-22 15:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.