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

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

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

前言

在 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 colorpaint.setTextSize(50); // Text size}public void setText(String text) {this.text = text;invalidate(); // Redraw the view}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);if (null != text && !text.isEmpty()) {float x = getWidth() / 2f; // Center horizontallyfloat y = getHeight() / 2f; // Center vertically// Save the canvas statecanvas.save();// Rotate the canvas by 90 degrees counterclockwisecanvas.rotate(-90, x, y);// Draw the text on the rotated canvascanvas.drawText(text, x - (paint.measureText(text) / 2), y, paint);// Restore the canvas statecanvas.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.VerticalTextViewandroid: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设置显示内容。

	@Overrideprotected 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"><TextViewandroid: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 neededpaint.setColor(0xFF000000); // Text color (black)paint.setTypeface(Typeface.MONOSPACE); // Set monospaced typeface}public void setText(String text) {this.text = text;invalidate();}@Overrideprotected 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.VerticalTextViewV2android: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设置显示内容。

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/15441.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

AI学习指南数学工具篇-凸优化基础知识凸函数

AI学习指南数学工具篇-凸优化基础知识凸函数 引言 在凸优化过程中&#xff0c;凸函数是一个非常重要的概念&#xff0c;它在机器学习、深度学习和优化算法中都有广泛的应用。凸函数具有很多独特的性质&#xff0c;能够帮助我们更好地理解优化问题并且设计高效的优化算法。本文…

【Golang】Golang获取Gin框架PostForm上传的文件

文章目录 前言一、函数解释二、代码实现三、总结 前言 在Web开发中&#xff0c;文件上传是一项常见的功能。例如&#xff0c;用户可能需要上传头像、文档或其他类型的文件。在Go语言的Gin框架中&#xff0c;我们可以很方便地处理文件上传。在这篇博客中&#xff0c;我将解释如…

怎样理解 Vue 的单项数据流

Vue 的单项数据流是一个核心概念&#xff0c;它指的是在 Vue 组件中&#xff0c;数据的流动方向是单向的&#xff0c;从父组件流向子组件。以下是关于 Vue 单项数据流的详细理解&#xff1a; 数据流的方向&#xff1a; Vue 中的数据流动是单向的&#xff0c;即数据只能从父组件…

中国交通信息科技集团有限公司(中交信科)java开发工程师-机试题目/颂大技术面试总结/理工数传 软件开发一面二面面试总结/武汉智能视觉信息技术有限公司/高级

武汉智能视觉信息技术有限公司/高级 如果解决jvm内存溢出如果解决亿级别的数据导出&#xff0c;有没有其他的方案可以解决呢索引的原理工作中用了哪些索引提高了多少的速度线程池的创建方法--解释new ThreadPool的其他参数以及四大拒绝策略分布式使用用到了哪些模式xxl-job的原…

pillow学习4

ImageChops 模块 在 Pillow 库的内置模块 ImageChops 中包含了多个用于实现图片合成的函数。这些合成 功能是通过计算通道中像素值的方式来实现的。其主要用于制作特效、合成图片等操作。 常用的内置函数如下所示&#xff1a; &#xff08;1&#xff09;相加函数 add()&#xf…

【Windows系统】解决Intel 6代CPU安装win7系统过程中无法操作键盘鼠标的问题

问题 微软表示&#xff0c;从 2016 年 7 月 17 日起&#xff0c;新的 Intel、AMD 和Qualcomm 处理器将仅支持 Windows 10&#xff0c;不再支持 Windows 7 和 8.1。因此&#xff0c;Intel 6代以后的CPU因为没有USB驱动无法完成win7系统的安装。 下文核心思想是通过老毛桃PE系统…

云界洞见:移动云服务开启技术创新与问题解决的新篇章

一、什么是移动云 移动云以“央企保障、安全智慧、算网一体、属地服务”为品牌支撑&#xff0c;聚焦智能算力建设&#xff0c;打造一朵智能、智慧、安全可信可控的云&#xff0c;提供更优质的算力服务&#xff0c;引领云计算产业发展。 那么下面博主带领大家了解移动云的优势所…

关于c++的通过cin.get()维持黑框的思考

1.前言 由于本科没有学过c语言&#xff0c;研究生阶段接触c上手有点困难&#xff0c;今天遇到关于通过cin.get()来让黑框维持的原因。 2.思考 cin.get()维持黑框不消失的原因一言蔽之就是等待输入。等待键盘的输入内容并回车&#xff08;一般是回车&#xff09;后cin.get()才…

Plotly库利用滑块创建数据可视化

使用了Plotly库来创建一个数据可视化图表&#xff0c;并使用滑块来控制显示哪些数据 import plotly.graph_objects as go from plotly.subplots import make_subplots# 示例数据 x [1, 2, 3, 4, 5] y1 [1, 2, 3, 4, 5] y2 [5, 4, 3, 2, 1] y3 [2, 3, 1, 5, 4]# 创建子图 f…

Python vscode debug: Error while enumerating installed packages.解决

记录一个vscode python debug时出现的错误&#xff1a; 具体错误如下&#xff1a; E00000.030: Error while enumerating installed packages. Traceback (most recent call last): File “/root/.vscode-server/extensions/ms-python.debugpy-2024.0.0-linux-x64/bundled/lib…

java —— 类与方法

一、访问修饰符 在类和方法中&#xff0c;均可使用访问修饰符以锁定该类或方法的被访问权限。访问修饰符有四种&#xff1a; &#xff08;一&#xff09;public 同一个项目中&#xff0c;对所有的类可见。 &#xff08;二&#xff09;protected 同一个项目中&#xff0c;对…

Study--Oracle-03-Oracle19C--RAC集群部署

一、硬件信息及配套软件 1、硬件设置 RAC集群虚拟机&#xff1a;CPU:2C、内存&#xff1a;9G、操作系统&#xff1a;30G、数据库安装目录&#xff1a;100G 数据存储&#xff1a;50G &#xff08;10G*5&#xff09; 共享存储&#xff1a;2G &#xff08;1G*2&#xff09; 2…

基于 vuestic-ui 实战教程

1. 前言简介 Vuestic UI是一个基于开源Vue 3的UI框架。它是一个MIT许可的UI框架&#xff0c;提供了易于配置的现成前端组件&#xff0c;并加快了响应式和快速加载Web界面的开发。它最初于2021年5月由EpicMax发布&#xff0c;这就是今天的Vuestic UI。 官网地址请点击访问 体验…

博客摘录「 python——正则表达式(re模块)详解」2023年11月17日

?P<name>) 分组起别名&#xff0c;匹配到的子串组在外部是通过定义的 name 来获取的(?Pname) 引⽤别名为name分组匹配到的字符串

车与网络之间(V2N)简介

车与网络之间&#xff08;V2N&#xff09;简介 一、定义与概述 V2N&#xff0c;全称为Vehicle-to-Network&#xff0c;是指车辆与网络之间的通信和连接技术。这种技术使得车辆能够与互联网进行无缝连接&#xff0c;进而实现导航、娱乐、防盗等多种应用功能。在智能交通系统领…

【Linux安全】iptables防火墙(二)

目录 一.iptables规则的保存 1.保存规则 2.还原规则 3.保存为默认规则 二.SNAT的策略及应用 1.SNAT策略的典型应用环境 2.SNAT策略的原理 2.1.未进行SNAT转换后的情况 2.2.进行SNAT转换后的情况 3.SNAT策略的应用 3.1.前提条件 3.2.实现方法 三.DNAT策略及应用 1…

【大模型应用开发极简入门】使用GPT-4和ChatGPT的编程起点:ChatCompletion详解

文章目录 一. 多轮对话二. 使用起点&#xff1a; ChatCompletion三. 调用模型&#xff1a;create方法1. 主要的输入参数&#xff1a;model、message2. 对话长度和token数量管理3. 可选参数 四. ChatCompletion端点的输出格式 本文讨论如何使用GPT-4和ChatGPT背后的模型&#xf…

怎么查看项目中antd的版本

使用antd时&#xff0c;有在线参考资料&#xff0c;但是需要根据项目需要&#xff0c;选择对应版本的参考资料。 antd在线参考资料&#xff1a; 组件总览 - Ant Design 如何查看当前项目中antd的版本呢&#xff1f; 在项目的终端中输入&#xff1a; npm list antd antd官网选择…

庆余年第2季,带你走进怎样的世界?

《庆余年》第二季 演员阵容与幕后团队的新组合为我们带来了别样的观影体验 他的演技真的是在线&#xff0c;其实这剧本很难搞 该搞笑的时候要搞笑&#xff0c;但也不能一直在无厘头胡闹 所以题主说节奏拿捏的好我也很赞同 反观有其他几位演员控制力就差很多 特别是某一集…

Spring:JWT

文章目录 一、介绍 一、介绍 JWT&#xff08;JSON Web Token&#xff09;是一种开放标准&#xff08;RFC 7519&#xff09;的方法&#xff0c;用于在双方之间安全地传输信息。这些信息可以是验证、授权、信息交换等。JWT 通常被用于在客户端和服务器之间传递用户信息&#xff…