当前位置:网站首页>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
边栏推荐
- Pagoda panel MySQL cannot be started
- 盗版DALL·E成梗图之王?日产5万张图像,挤爆抱抱脸服务器,OpenAI勒令改名
- A high density 256 channel electrode cap for dry EEG
- oracle和mysql批量Merge对比
- Dedecms website building tutorial
- La voie de l'évolution du système intelligent d'inspection et d'ordonnancement des petites procédures de Baidu
- 字节跳动面试官:一张图片占据的内存大小是如何计算
- Using directive in angualr2 to realize that the picture size changes with the window size
- Click the picture in the mobile browser and the picture will not pop up
- Is it really reliable for AI to make complex decisions for enterprises? Participate in the live broadcast, Dr. Stanford to share his choice | qubit · viewpoint
猜你喜欢
Apache dolphin scheduler system architecture design
ArcGIS Pro creating features
(1) Complete the new construction of station in Niagara vykon N4 supervisor 4.8 software
Swift set pickerview to white on black background
Coordinate system of view
La voie de l'évolution du système intelligent d'inspection et d'ordonnancement des petites procédures de Baidu
@SerializedName注解使用
硬核,你见过机器人玩“密室逃脱”吗?(附代码)
Small program startup performance optimization practice
[C language] the use of dynamic memory development "malloc"
随机推荐
Cross process communication Aidl
宝塔面板MySQL无法启动
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package is directly used - on
[200 opencv routines] 219 Add digital watermark (blind watermark)
[NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
Swift set pickerview to white on black background
Dedecms website building tutorial
剪掉ImageNet 20%数据量,模型性能不下降!Meta斯坦福等提出新方法,用知识蒸馏给数据集瘦身...
View Slide
Meitu lost 300 million yuan in currency speculation for half a year. Huawei was exposed to expand its enrollment in Russia. Alphago's peers have made another breakthrough in chess. Today, more big new
Charm of code language
(1) Complete the new construction of station in Niagara vykon N4 supervisor 4.8 software
能源势动:电力行业的碳中和该如何实现?
天龙八部TLBB系列 - 关于技能冷却和攻击范围数量的问题
让AI替企业做复杂决策真的靠谱吗?参与直播,斯坦福博士来分享他的选择|量子位·视点...
Advanced opencv:bgr pixel intensity map
Design of stepping motor controller based on single chip microcomputer (forward rotation and reverse rotation indicator gear)
Flutter development: a way to solve the problem of blank space on the top of listview
历史上的今天:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生...
TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)