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

相关推荐
千里马学框架2 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台2 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone2 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc2 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo2 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077002 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼2 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone2 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen2 天前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone2 天前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui