当前位置:网站首页>StaticLayout的使用详解
StaticLayout的使用详解
2022-06-28 00:57:00 【沙漠一只雕得儿得儿】
在Android开发中,Canvas.drawText不会换行,即使一个很长的字符串也只会显示一行,超出部分会隐藏在屏幕之外。
StaticLayout是android中处理文字的一个工具类,StaticLayout 处理了文字换行的问题,如如下示例:
package com.zhy.someuitest;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
public class StaticLayoutView extends View {
StaticLayout staticLayout;
public StaticLayoutView(Context context) {
this(context, null);
}
public StaticLayoutView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public StaticLayoutView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(Color.BLACK);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setTextSize(50);
String text = "在Android开发中,Canvas.drawText不会换行,即使一个很长的字符串也只会显示一行,超出部分会隐藏在屏幕之外.StaticLayout是android中处理文字的一个工具类,StaticLayout 处理了文字换行的问题";
staticLayout = new StaticLayout(text, textPaint, canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
staticLayout.draw(canvas);
}
}

Staticlayout的中构造参数有三个:
以 public StaticLayout(CharSequence source, int bufstart, int bufend,
TextPaint paint, int outerwidth,
Alignment align,
float spacingmult, float spacingadd,
boolean includepad,
TextUtils.TruncateAt ellipsize, int ellipsizedWidth) {
this(source, bufstart, bufend, paint, outerwidth, align,
TextDirectionHeuristics.FIRSTSTRONG_LTR,
spacingmult, spacingadd, includepad, ellipsize, ellipsizedWidth, Integer.MAX_VALUE);
}说明参数的作用
CharSequence source 需要分行的字符串
int bufstart 需要分行的字符串从第几的位置开始
int bufend 需要分行的字符串到哪里结束
TextPaint paint 画笔对象
int outerwidth layout的宽度,超出时换行
Alignment align layout的对其方式,有ALIGN_CENTER, ALIGN_NORMAL, ALIGN_OPPOSITE 三种
float spacingmult 相对行间距,相对字体大小,1.5f表示行间距为1.5倍的字体高度。
float spacingadd 在基础行距上添加多少
boolean includepad,
TextUtils.TruncateAt ellipsize 从什么位置开始省略
int ellipsizedWidth 超过多少开始省略
补充:TextView其实也是使用 StaticLayout
边栏推荐
- Win11如何关闭最近打开项目?Win11关闭最近打开项目的方法
- Cloud platform KVM migration local virtual machine records
- SQL injection bypass (IV)
- js实现时钟
- MySQL query related knowledge (advanced level 7: sub query
- fiddle如何使用代理
- 【历史上的今天】6 月 23 日:图灵诞生日;互联网奠基人出生;Reddit 上线
- 「大道智创」获千万级preA+轮融资,推出科技消费机器人
- 【二维码图像矫正增强】基于MATLAB的二维码图像矫正增强处理仿真
- 关于st-link usb communication error的解决方法
猜你喜欢

Protocole de transfert de fichiers - - FTP

How to enable multi language text suggestions? Win11 method to open multilingual text suggestions

Desai wisdom number - histogram (column folding mixed graph): ratio of rental price to rental income in the graduation quarter of 2021

榜单首发——前装搭载率站上10%大关,数字钥匙方案供应商TOP10

Win11如何关闭最近打开项目?Win11关闭最近打开项目的方法

迪赛智慧数——柱状图(折柱混合图):2021年毕业季租房价格和房租收入比

【历史上的今天】6 月 20 日:MP3 之父出生;富士通成立;谷歌收购 Dropcam

【历史上的今天】6 月 16 日:甲骨文成立;微软 MSX 诞生;快速傅里叶变换发明者出生

Moving Tencent to the cloud: half of the evolution history of cloud server CVM
![[today in history] June 5: Lovelace and Babbage met; The pioneer of public key cryptography was born; Functional language design pioneer born](/img/af/1faf1df4262940e73104ed2880f08d.png)
[today in history] June 5: Lovelace and Babbage met; The pioneer of public key cryptography was born; Functional language design pioneer born
随机推荐
Exploration on the construction path of real-time digital warehouse integrating digital intelligence learning and streaming batch
云原生(三十) | Kubernetes篇之应用商店-Helm
[today in history] June 2: Apple launched swift programming language; China Telecom acquires China Unicom C network; OS X Yosemite release
【模糊神经网络】基于matlab的模糊神经网络仿真
Win11 cannot create a new text document? Solution to win11 right click failure to create a new text document
How to systematically learn LabVIEW?
fiddle如何使用代理
如何系统学习LabVIEW?
【历史上的今天】5 月 31 日:Amiga 之父诞生;BASIC 语言的共同开发者出生;黑莓 BBM 停运
原理图合并中的技巧
> Could not create task ‘:app:MyTest.main()‘. > SourceSet with name ‘main‘ not found.问题修复
Solutions to st link USB communication error
Step by step interpretation of crf+bilstm code
"Dadao Zhichuang" won a ten million prea+ round of financing and launched a technology consumption robot
【历史上的今天】6 月 15 日:第一个手机病毒;AI 巨匠司马贺诞生;Chromebook 发布
【历史上的今天】6 月 7 日:Kubernetes 开源版本发布;《魔兽世界》登陆中国;分组交换网络发明者出生
如何开启多语言文本建议?Win11打开多语言文本建议的方法
Win11不能拖拽图片到任务栏软件上快速打开怎么办
【历史上的今天】6 月 3 日:微软推出必应搜索引擎;Larry Roberts 启动阿帕网;Visual Basic 之父出生
SQL injection bypass (IV)