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.pswp.cn/web/15441.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/15441.shtml
英文地址,請注明出處:http://en.pswp.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…