当前位置:网站首页>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
边栏推荐
- NER中BiLSTM-CRF解读Forward_algorithm
- 在线JSON转PlainText工具
- [today in history] May 31: the father of Amiga was born; The co developer of basic language was born; BlackBerry BBM shutdown
- 【历史上的今天】6 月 10 日:Apple II 问世;微软收购 GECAD;发明“软件工程”一词的科技先驱出生
- Flutter 使用 CustomPaint 绘制基本图形
- fiddle如何使用代理
- 【电梯控制系统】基于VHDL语言和状态机实现的电梯控制系统的设计,使用了状态机
- What if win11 can't drag an image to the taskbar software to open it quickly
- Opencv——几何空间变换(仿射变换和投影变换)
- Mysql database foundation: DML data operation language
猜你喜欢
![[JS reverse hundreds of examples] I love to solve 2022 Spring Festival problems and receive red envelopes](/img/9b/4e0b5838f8c272e9fbc25eb8ef5efa.jpg)
[JS reverse hundreds of examples] I love to solve 2022 Spring Festival problems and receive red envelopes

【历史上的今天】6 月 15 日:第一个手机病毒;AI 巨匠司马贺诞生;Chromebook 发布

【历史上的今天】5 月 31 日:Amiga 之父诞生;BASIC 语言的共同开发者出生;黑莓 BBM 停运

【历史上的今天】6 月 6 日:世界 IPv6 启动纪念日;《俄罗斯方块》发布;小红书成立

【历史上的今天】6 月 23 日:图灵诞生日;互联网奠基人出生;Reddit 上线

STM32F1与STM32CubeIDE编程实例-金属触摸传感器驱动

【历史上的今天】6 月 2 日:苹果推出了 Swift 编程语言;电信收购联通 C 网;OS X Yosemite 发布

设计电商秒杀系统

【历史上的今天】6 月 10 日:Apple II 问世;微软收购 GECAD;发明“软件工程”一词的科技先驱出生

如何开启多语言文本建议?Win11打开多语言文本建议的方法
随机推荐
转载文章:数字经济催生强劲算力需求 英特尔发布多项创新技术挖掘算力潜能
JS implementation clock
[today in history] June 1: Napster was founded; MS-DOS original author was born; Google sells Google SketchUp
【历史上的今天】6 月 8 日:万维网之父诞生;PHP 公开发布;iPhone 4 问世
毕业总结
isEmpty 和 isBlank 的用法区别
「大道智创」获千万级preA+轮融资,推出科技消费机器人
Win11 cannot create a new text document? Solution to win11 right click failure to create a new text document
JS实现滑动拼图验证
Design e-commerce seckill system
fiddle如何使用代理
【历史上的今天】6 月 5 日:洛夫莱斯和巴贝奇相遇;公钥密码学先驱诞生;函数语言设计先驱出生
How does win11 close recently opened projects? Win11 method to close recently opened projects
flask基础:模板继承+静态文件配置
在线JSON转PlainText工具
Truth table of common anode digital tube
【历史上的今天】6 月 17 日:术语“超文本”的创造者出生;Novell 首席科学家诞生;探索频道开播
NER中BiLSTM-CRF解读Forward_algorithm
【二维码图像矫正增强】基于MATLAB的二维码图像矫正增强处理仿真
Is it safe for qiniu to open an account? How do I open an account online?