当前位置:网站首页>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
边栏推荐
- STM32F103的11个定时器
- Opencv——霍夫变换以及遇到的一些问题
- SQL reported an unusual error, which confused the new interns
- Win11如何关闭最近打开项目?Win11关闭最近打开项目的方法
- [today in history] May 31: the father of Amiga was born; The co developer of basic language was born; BlackBerry BBM shutdown
- Moving Tencent to the cloud: half of the evolution history of cloud server CVM
- 第三批入围企业公示!年度TOP100智能网联供应商评选
- 【历史上的今天】6 月 7 日:Kubernetes 开源版本发布;《魔兽世界》登陆中国;分组交换网络发明者出生
- Solutions to st link USB communication error
- There appears to be a failure with your network connection Retrying.
猜你喜欢
How to systematically learn LabVIEW?
From how to use to how to implement a promise
Protocole de transfert de fichiers - - FTP
Interpretation of bilstm-crf in NER forward_ algorithm
【二維碼圖像矯正增强】基於MATLAB的二維碼圖像矯正增强處理仿真
Win11新建不了文本文档?Win11右键无法新建文本文档的解决方法
Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times
[fuzzy neural network] simulation of fuzzy neural network based on MATLAB
Moving Tencent to the cloud: half of the evolution history of cloud server CVM
File transfer protocol --ftp
随机推荐
【历史上的今天】5 月 31 日:Amiga 之父诞生;BASIC 语言的共同开发者出生;黑莓 BBM 停运
【历史上的今天】6 月 8 日:万维网之父诞生;PHP 公开发布;iPhone 4 问世
I forgot my cell phone today
Mysql大合集,你要内容的这里全都有
How to enable multi language text suggestions? Win11 method to open multilingual text suggestions
迪赛智慧数——柱状图(折柱混合图):2021年毕业季租房价格和房租收入比
flask基础:模板继承+静态文件配置
云原生(三十) | Kubernetes篇之应用商店-Helm
[today in history] June 7: kubernetes open source version was released; Worldofwarcraft landed in China; Birth of the inventor of packet switching network
第三批入围企业公示!年度TOP100智能网联供应商评选
Win11如何关闭最近打开项目?Win11关闭最近打开项目的方法
【二维码图像矫正增强】基于MATLAB的二维码图像矫正增强处理仿真
js实现时钟
STM32F103的11个定时器
Cvpr22 collected papers | hierarchical residual multi granularity classification network based on label relation tree
Win11不能拖拽圖片到任務欄軟件上快速打開怎麼辦
【倒立摆控制】基于UKF无迹卡尔曼滤波的倒立摆控制simulink仿真
Cloud native (30) | kubernetes' app store Helm
被通知裁员后拿到5个offer
启牛开户安全吗?怎么线上开户?