当前位置:网站首页>StaticLayout的使用详解
StaticLayout的使用详解
2022-07-05 09:41: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
边栏推荐
- mysql80服务不启动
- Fluent generates icon prompt logo widget
- Unity particle special effects series - the poison spray preform is ready, and the unitypackage package is directly used - on
- Apache DolphinScheduler 系统架构设计
- 分布式数据库下子查询和 Join 等复杂 SQL 如何实现?
- Theme. AppCompat. Light. Darkactionbar not found
- Comparison of batch merge between Oracle and MySQL
- Mobile heterogeneous computing technology GPU OpenCL programming (Advanced)
- Using directive in angualr2 to realize that the picture size changes with the window size
- Tianlong Babu TLBB series - questions about skill cooling and the number of attack ranges
猜你喜欢
ArcGIS Pro 创建要素
单片机原理与接口技术(ESP8266/ESP32)机器人类草稿
如何获取GC(垃圾回收器)的STW(暂停)时间?
[technical live broadcast] how to rewrite tdengine code from 0 to 1 with vscode
Evolution of Baidu intelligent applet patrol scheduling scheme
Officially launched! Tdengine plug-in enters the official website of grafana
硬核,你见过机器人玩“密室逃脱”吗?(附代码)
How to get the STW (pause) time of GC (garbage collector)?
如何写出高质量的代码?
Mysql80 service does not start
随机推荐
[system design] index monitoring and alarm system
驱动制造业产业升级新思路的领域知识网络,什么来头?
Node-RED系列(二九):使用slider与chart节点来实现双折线时间序列图
Uncover the practice of Baidu intelligent testing in the field of automatic test execution
Comparison of batch merge between Oracle and MySQL
The essence of persuasion is to remove obstacles
《微信小程序-基础篇》小程序中的事件与冒泡
自动化规范检查软件如何发展而来?
RMS TO EAP通过MQTT简单实现
Tianlong Babu TLBB series - questions about skill cooling and the number of attack ranges
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package is directly used - on
一个程序员的职业生涯到底该怎么规划?
Swift uses userdefaults and codable to save an array of class objects or structure instances
Generics, generic defects and application scenarios that 90% of people don't understand
Solve liquibase – waiting for changelog lock Cause database deadlock
卷起来,突破35岁焦虑,动画演示CPU记录函数调用过程
Small program startup performance optimization practice
Cut off 20% of Imagenet data volume, and the performance of the model will not decline! Meta Stanford et al. Proposed a new method, using knowledge distillation to slim down the data set
Cerebral Cortex:有向脑连接识别帕金森病中广泛存在的功能网络异常
苹果 5G 芯片研发失败?想要摆脱高通为时过早