Android的TextView在显示文字的时候,如果有段中文有英文,有中文,有中文标点符号,你会发现,当要换行的时候遇到中文标点, 这一行就会空出很多空格出来...
一、問題描述:
Android的TextView在顯示文字的時(shí)候,如果有段中文有英文,有中文,有中文標(biāo)點(diǎn)符號(hào),你會(huì)發(fā)現(xiàn),當(dāng)要換行的時(shí)候遇到中文標(biāo)點(diǎn),
這一行就會(huì)空出很多空格出來。原因是:
?
1) TextView在顯示中文的時(shí)候 標(biāo)點(diǎn)符號(hào)不能顯示在一行的行首和行尾,如果一個(gè)標(biāo)點(diǎn)符號(hào)剛好在一行的行尾,該標(biāo)點(diǎn)符號(hào)就會(huì)連同前一個(gè)字符跳到下一行顯示;
2)一個(gè)英文單詞不能被顯示在兩行中( TextView在顯示英文時(shí),標(biāo)點(diǎn)符號(hào)是可以放在行尾的,但英文單詞也不能分開 );
3)全角和半角的問題,漢字無論全角還是半角都是占2個(gè)字節(jié),英文和符號(hào)在半角是占一個(gè)字節(jié),全角是占兩個(gè)字節(jié)。
二、解決方法
參考資料中:http://niufc.iteye.com/blog/1729792?
可能由于時(shí)間問題,都沒有很好解決我的問題。將textview中的字符全角化沒有效果,去除特殊字符或?qū)⑺兄形臉?biāo)號(hào)替換為英文標(biāo)號(hào)。這個(gè)有點(diǎn)效果,但是產(chǎn)品經(jīng)理說文案不符合標(biāo)準(zhǔn)。改源代碼擔(dān)心出問題,影響其他的應(yīng)用。自定義TextView時(shí),canvas.setViewport()這個(gè)方法的api被刪了。然后各種百度查資料,很多都是轉(zhuǎn)過來轉(zhuǎn)過去。然并卵。后面找了好久才找到一個(gè)靠譜的。完美的解決了我的問題。
?
自定義TextView(直接把代碼拷進(jìn)去就能用)
package com.lhx.widget;
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;
/**
* @author lhx
* @Date 9/8/15
*/
public class MyTextView extends TextView {
private int mLineY;
private int mViewWidth;
public MyTextView(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出現(xiàn)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';
}
}
}
?
<!--工作時(shí)間提醒-->
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/ll_chat"
android:background="@color/topbar_chat"
android:layout_height="wrap_content">
<com.lhx.widget.MyTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_reminder"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:text = "服務(wù)商不管以任何形式要求線下交易,都存在詐騙的風(fēng)險(xiǎn),請?zhí)岣呔琛his is test!歡迎相互關(guān)注。有不對的地方望指出和包容。謝謝! "
android:textColor="@color/text_orange_1"/>
</LinearLayout>
?
?
以上就很好的解決了TextView中英文字符排版的問題,折騰了好久終于搞定。希望對您有點(diǎn)幫助。
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/lenkevin/p/8940237.html
總結(jié)
以上是生活随笔為你收集整理的Android的TextView在显示文字的时候,如果有段中文有英文,有中文,有中文标点符号,你会发现,当要换行的时候遇到中文标点, 这一行就会空出很多空格出来...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python创建数据库的sql语句_对p
- 下一篇: 简单的文件上传功能实现(java)