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

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

  • 前言
  • 效果图
  • 代码实现
    • [方式一 Custom View](#方式一 Custom View)
      • [1. 自定义视图 VerticalTextView](#1. 自定义视图 VerticalTextView)
      • [2. 在xml布局文件中使用](#2. 在xml布局文件中使用)
      • [3. 设置文本内容](#3. 设置文本内容)
    • [方式二 使用 TextView 的 rotation属性](#方式二 使用 TextView 的 rotation属性)
    • [方式三 使用带有跨距文本的TextView](#方式三 使用带有跨距文本的TextView)
      • [1. 自定义视图 VerticalTextView](#1. 自定义视图 VerticalTextView)
      • [2. 在xml布局文件中使用](#2. 在xml布局文件中使用)
      • [3. 设置文本内容](#3. 设置文本内容)
  • 总结

前言

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

效果图

代码实现

方式一 Custom View

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

1. 自定义视图 VerticalTextView

java 复制代码
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 复制代码
<?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设置显示内容。

java 复制代码
	@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

java 复制代码
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 复制代码
<?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设置显示内容。

java 复制代码
@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 是最简单、最快的方法。使用时选择最适合你的一种。

相关推荐
有位神秘人15 小时前
Android中Notification的使用详解
android·java·javascript
·云扬·15 小时前
MySQL Binlog落盘机制深度解析:性能与安全性的平衡艺术
android·mysql·adb
独自破碎E16 小时前
【BISHI9】田忌赛马
android·java·开发语言
代码s贝多芬的音符17 小时前
android 两个人脸对比 mlkit
android
darkb1rd19 小时前
五、PHP类型转换与类型安全
android·安全·php
gjxDaniel20 小时前
Kotlin编程语言入门与常见问题
android·开发语言·kotlin
csj5020 小时前
安卓基础之《(22)—高级控件(4)碎片Fragment》
android
峥嵘life20 小时前
Android16 【CTS】CtsMediaCodecTestCases等一些列Media测试存在Failed项
android·linux·学习
stevenzqzq21 小时前
Compose 中的状态可变性体系
android·compose
似霰21 小时前
Linux timerfd 的基本使用
android·linux·c++