Android 实现竖排文本(垂直方向显示)

前言

在 Android 应用程序中显示垂直文本可以通过多种方法实现,具体取决于项目的复杂性和要求。以下介绍在 Android 中显示垂直文本的几种方法。

效果图

在这里插入图片描述

代码实现

方式一 Custom View

自定义view,创建一个重载 onDraw 方法的自定义view,用来垂直绘制文本。

1. 自定义视图 VerticalTextView

public class VerticalTextView extends View {

    private String text = "";
    private Paint paint;


    public VerticalTextView(Context context) {
        super(context);
        init();
    }

    public VerticalTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public VerticalTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init() {
        paint = new Paint();
        paint.setColor(0xFF000000); // Black color
        paint.setTextSize(50); // Text size
    }

    public void setText(String text) {
        this.text = text;
        invalidate(); // Redraw the view
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (null != text && !text.isEmpty()) {
            float x = getWidth() / 2f; // Center horizontally
            float y = getHeight() / 2f; // Center vertically

            // Save the canvas state
            canvas.save();

            // Rotate the canvas by 90 degrees counterclockwise
            canvas.rotate(-90, x, y);

            // Draw the text on the rotated canvas
            canvas.drawText(text, x - (paint.measureText(text) / 2), y, paint);

            // Restore the canvas state
            canvas.restore();
        }
    }
}

2. 在xml布局文件中使用

你可以在布局 XML 文件中使用自定义 VerticalTextView

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".verticalText.VerticalTextActivity">

    <com.csu.verticalText.VerticalTextView
        android:id="@+id/verticalTextView"
        android:layout_width="60dp"
        android:layout_height="155dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

3. 设置文本内容

你可以在ActivityFragment中,为VerticalTextView设置显示内容。

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vertical_text);

        VerticalTextView verticalTextView = findViewById(R.id.verticalTextView);
        verticalTextView.setText("Hello World");
    }

方式二 使用 TextView 的 rotation属性

另一种更简单的方法是使用标准的 TextView 并将其旋转 90 度。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".verticalText.VerticalTextActivity">
    
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="55dp"
        android:text="Hello World"
        android:rotation="270"
        android:textSize="18sp"
        android:textColor="#353535"
        android:layout_marginTop="50dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

方式三 使用带有跨距文本的TextView

如果需要对布局进行更多控制,可以使用 SpannableString 使每个字符垂直对齐。

1. 自定义视图 VerticalTextView

public class VerticalTextViewV2 extends View {
    private String text = "Vertical Text";
    private Paint paint;

    public VerticalTextViewV2(Context context) {
        super(context);
        init();
    }

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

    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextSize(48); // Adjust the text size as needed
        paint.setColor(0xFF000000); // Text color (black)
        paint.setTypeface(Typeface.MONOSPACE); // Set monospaced typeface
    }

    public void setText(String text) {
        this.text = text;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float charHeight = paint.getTextSize();
        float x = getWidth() / 2f;
        for (int i = 0; i < text.length(); i++) {
            float y = charHeight * (i + 1);
            canvas.drawText(String.valueOf(text.charAt(i)), x, y, paint);
        }
    }

}

2. 在xml布局文件中使用

你可以在布局 XML 文件中使用自定义 VerticalTextView

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".verticalText.VerticalTextActivity">
    
    <com.csu.verticalText.VerticalTextViewV2
        android:id="@+id/verticalTextViewV2"
        android:layout_width="60dp"
        android:layout_height="220dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

3. 设置文本内容

你可以在ActivityFragment中,为VerticalTextView设置显示内容。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vertical_text);

        VerticalTextViewV2 verticalTextViewV2 = findViewById(R.id.verticalTextViewV2);
        verticalTextViewV2.setText("Hello World");
    }

总结

这些方法应该涵盖了 Android 中显示竖排文本的大部分场景。自定义视图方法提供了最大的灵活性,而旋转 TextView 是最简单、最快的方法。使用时选择最适合你的一种。

  • 19
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值