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

相关推荐
Reese_Cool1 小时前
【C语言二级考试】循环结构设计
android·java·c语言·开发语言
平凡シンプル1 小时前
安卓 uniapp跨端开发
android·uni-app
elina80131 小时前
安卓实现导入Excel文件
android·excel
严文文-Chris2 小时前
【设计模式-享元】
android·java·设计模式
趋势大仙2 小时前
SQLiteDatabase insert or replace数据不生效
android·数据库
DS小龙哥2 小时前
QT For Android开发-打开PPT文件
android·qt·powerpoint
试行3 小时前
Android实现自定义下拉列表绑定数据
android·java
Dingdangr8 小时前
Android中的Intent的作用
android
技术无疆8 小时前
快速开发与维护:探索 AndroidAnnotations
android·java·android studio·android-studio·androidx·代码注入
GEEKVIP8 小时前
Android 恢复挑战和解决方案:如何从 Android 设备恢复删除的文件
android·笔记·安全·macos·智能手机·电脑·笔记本电脑