Android 解决TextView排版参差不齐的问题
作者:lqh 时间:2022-06-25 06:43:30
Android 解决TextView排版参差不齐的问题
在app中,展示数据时,里面有汉字、数字、特殊字符时,由于全角、半角问题导致TextView参差不齐。在网上找了许多,半角转全角并没什么用,还有其他自定义TextView都有问题。最后终于找到一个,就像Word一样,可以使文字左右两端对齐:
package com.monkey.monkeymushroom.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* 解决文字排版混乱参差不齐的问题
*/
public class JustifyTextView extends TextView {
private int mLineY;
private int mViewWidth;
public static final String TWO_CHINESE_BLANK = " ";
public JustifyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
TextPaint paint = getPaint();
paint.setColor(getCurrentTextColor());
paint.drawableState = getDrawableState();
mViewWidth = getMeasuredWidth();
String text = getText().toString();
mLineY = 0;
mLineY += getTextSize();
Layout layout = getLayout();
// layout.getLayout()在4.4.3出现NullPointerException
if (layout == null) {
return;
}
Paint.FontMetrics fm = paint.getFontMetrics();
int textHeight = (int) (Math.ceil(fm.descent - fm.ascent));
textHeight = (int) (textHeight * layout.getSpacingMultiplier() + layout
.getSpacingAdd());
//解决了最后一行文字间距过大的问题
for (int i = 0; i < layout.getLineCount(); i++) {
int lineStart = layout.getLineStart(i);
int lineEnd = layout.getLineEnd(i);
float width = StaticLayout.getDesiredWidth(text, lineStart,
lineEnd, getPaint());
String line = text.substring(lineStart, lineEnd);
if (i < layout.getLineCount() - 1) {
if (needScale(line)) {
drawScaledText(canvas, lineStart, line, width);
} else {
canvas.drawText(line, 0, mLineY, paint);
}
} else {
canvas.drawText(line, 0, mLineY, paint);
}
mLineY += textHeight;
}
}
private void drawScaledText(Canvas canvas, int lineStart, String line, float lineWidth) {
float x = 0;
if (isFirstLineOfParagraph(lineStart, line)) {
String blanks = " ";
canvas.drawText(blanks, x, mLineY, getPaint());
float bw = StaticLayout.getDesiredWidth(blanks, getPaint());
x += bw;
line = line.substring(3);
}
int gapCount = line.length() - 1;
int i = 0;
if (line.length() > 2 && line.charAt(0) == 12288
&& line.charAt(1) == 12288) {
String substring = line.substring(0, 2);
float cw = StaticLayout.getDesiredWidth(substring, getPaint());
canvas.drawText(substring, x, mLineY, getPaint());
x += cw;
i += 2;
}
float d = (mViewWidth - lineWidth) / gapCount;
for (; i < line.length(); i++) {
String c = String.valueOf(line.charAt(i));
float cw = StaticLayout.getDesiredWidth(c, getPaint());
canvas.drawText(c, x, mLineY, getPaint());
x += cw + d;
}
}
private boolean isFirstLineOfParagraph(int lineStart, String line) {
return line.length() > 3 && line.charAt(0) == ' '
&& line.charAt(1) == ' ';
}
private boolean needScale(String line) {
if (line == null || line.length() == 0) {
return false;
} else {
return line.charAt(line.length() - 1) != '\n';
}
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
标签:Android,解决,TextView排版
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Maven中怎么手动添加jar包到本地仓库详解(repository)
2023-09-08 07:33:20
![](https://img.aspxhome.com/file/2023/3/82013_0s.jpg)
java如何对map进行排序详解(map集合的使用)
2022-12-22 18:26:13
jtds1.1连接sqlserver2000测试示例
2021-06-13 04:57:28
使用Java实现Redis限流的方法
2023-09-27 01:43:47
![](https://img.aspxhome.com/file/2023/9/77999_0s.png)
SpringMVC @RequestBody的使用解析
2022-01-30 06:34:32
![](https://img.aspxhome.com/file/2023/1/62761_0s.jpg)
C# WebApi 异常处理解决方案
2021-06-14 16:52:08
![](https://img.aspxhome.com/file/2023/1/77261_0s.png)
java中单例模式讲解
2022-05-22 14:24:07
![](https://img.aspxhome.com/file/2023/5/60335_0s.png)
java实现将数字转换成人民币大写
2023-08-11 05:07:29
Java8之lambda最佳实践_动力节点Java学院整理
2023-11-28 00:07:28
Javassist用法详解
2023-06-26 17:11:54
Jenkins安装以及邮件配置详解
2023-04-20 12:42:39
![](https://img.aspxhome.com/file/2023/1/63701_0s.png)
Maven引入外部jar的几种方法(小结)
2022-11-10 01:35:30
springboot项目以jar包运行的操作方法
2023-11-10 07:47:43
![](https://img.aspxhome.com/file/2023/8/59328_0s.png)
SpringBoot中JPA实现Sort排序的三种方式小结
2022-02-12 23:35:12
Mybatis如何实现@Select等注解动态组合SQL语句
2022-04-13 16:42:05
C#仪器数据文件解析Excel文件的方法浅析(xls、xlsx)
2023-09-18 01:40:57
Java之SpringBoot自定义配置与整合Druid
2022-09-28 06:23:06
![](https://img.aspxhome.com/file/2023/7/63007_0s.png)
深入理解C#中foreach遍历的使用方法
2023-11-02 08:51:04
![](https://img.aspxhome.com/file/2023/5/82135_0s.jpg)
unity 文件流读取图片与www读取图片的区别介绍
2023-08-30 09:10:37
![](https://img.aspxhome.com/file/2023/9/89769_0s.jpg)
解决SpringBoot加载application.properties配置文件的坑
2023-02-05 19:55:27
![](https://img.aspxhome.com/file/2023/5/85435_0s.png)