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

相关推荐
椰羊sqrt2 小时前
CVE-2025-4334 深度分析:WordPress wp-registration 插件权限提升漏洞
android·开发语言·okhttp·网络安全
2501_916008892 小时前
金融类 App 加密加固方法,多工具组合的工程化实践(金融级别/IPA 加固/无源码落地/Ipa Guard + 流水线)
android·ios·金融·小程序·uni-app·iphone·webview
sun0077002 小时前
Android设备推送traceroute命令
android
来来走走3 小时前
Android开发(Kotlin) 高阶函数、内联函数
android·开发语言·kotlin
2501_915921433 小时前
Fastlane 结合 开心上架(Appuploader)命令行版本实现跨平台上传发布 iOS App 免 Mac 自动化上架实战全解析
android·macos·ios·小程序·uni-app·自动化·iphone
雨白3 小时前
重识 Java IO、NIO 与 OkIO
android·java
啦啦9117144 小时前
Niagara Launcher 全新Android桌面启动器!给手机换个门面!
android·智能手机
游戏开发爱好者84 小时前
iOS 上架要求全解析,App Store 审核标准、开发者准备事项与开心上架(Appuploader)跨平台免 Mac 实战指南
android·macos·ios·小程序·uni-app·iphone·webview
xrkhy4 小时前
canal1.1.8+mysql8.0+jdk17+redis的使用
android·redis·adb
00后程序员张5 小时前
混淆 iOS 类名与变量名的实战指南,多工具组合把混淆做成工程能力(混淆 iOS 类名变量名/IPA 成品混淆Ipa/Guard CLI 实操)
android·ios·小程序·https·uni-app·iphone·webview