当前位置:   article > 正文

Android解决textview遇到标点自动换行的问题_android textview 邮箱标点会换行

android textview 邮箱标点会换行

Android的TextView在显示文字的时候有个问题就是一行还没显示满就跳到下一行,原因是:

1) TextView在显示中文的时候 标点符号不能显示在一行的行首和行尾,如果一个标点符号刚好在一行的行尾,该标点符号就会连同前一个字符跳到下一行显示;

2)一个英文单词不能被显示在两行中( TextView在显示英文时,标点符号是可以放在行尾的,但英文单词也不能分开 );

 

如果只是想让标点符号可以显示在行尾,有一个简单的方法就是在标点符号后加一个空格,则该标点符号就可以显示在行尾了;

具体解决方法,直接上代码:

  1. import android.annotation.TargetApi;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Paint;
  5. import android.os.Build;
  6. import android.support.annotation.ColorRes;
  7. import android.support.annotation.DimenRes;
  8. import android.util.AttributeSet;
  9. import android.view.View;
  10. import com.pj.register.R;
  11. import com.pj.register.application.MyApplication;
  12. import java.util.ArrayList;
  13. /**
  14. * Created by wzl on 2016/6/24.
  15. */
  16. public class CustomTextView extends View {
  17. private Paint mPaint = null;
  18. private Paint.FontMetrics fm;
  19. private float offset;
  20. private String content = "测试数据";
  21. private float lineSpace = 0;
  22. public CustomTextView(Context context) {
  23. super(context);
  24. init();
  25. }
  26. public CustomTextView(Context context, AttributeSet set) {
  27. super(context, set, 0);
  28. init();
  29. }
  30. public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
  31. super(context, attrs, defStyleAttr);
  32. init();
  33. }
  34. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  35. public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  36. super(context, attrs, defStyleAttr, defStyleRes);
  37. init();
  38. }
  39. private void init() {
  40. mPaint = new Paint();
  41. mPaint.setAntiAlias(true);
  42. mPaint.setColor(getResources().getColor(R.color.black));
  43. mPaint.setTextSize(dip2px(16));
  44. initParams();
  45. }
  46. private void initParams() {
  47. fm = mPaint.getFontMetrics();
  48. if (lineSpace > 0) {
  49. fm.leading = lineSpace;
  50. }else{
  51. fm.leading = dip2px(1);
  52. }
  53. offset = fm.descent - fm.ascent + fm.leading;
  54. }
  55. public static int dip2px(float dpValue) {
  56. final float scale = MyApplication.getInstance().getResources().getDisplayMetrics().density;
  57. return (int) (dpValue * scale + 0.5f);
  58. }
  59. @Override
  60. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  61. int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  62. int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  63. int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  64. int heightSize = MeasureSpec.getSize(heightMeasureSpec);
  65. int width;
  66. int height;
  67. if (widthMode == MeasureSpec.EXACTLY) {
  68. width = widthSize;
  69. } else if (widthMode == MeasureSpec.AT_MOST) {
  70. int textWidth = (int) mPaint.measureText(content);
  71. width = textWidth + getPaddingLeft() + getPaddingRight() > widthSize ? widthSize : textWidth + getPaddingLeft() + getPaddingRight();
  72. } else {
  73. int textWidth = (int) mPaint.measureText(content);
  74. width = textWidth + getPaddingLeft() + getPaddingRight();
  75. }
  76. if (heightMode == MeasureSpec.EXACTLY) {
  77. height = heightSize;
  78. } else if (heightMode == MeasureSpec.AT_MOST) {
  79. int lines = calculateLines(content, width - getPaddingLeft() - getPaddingRight()).size();
  80. int indeedHeight = getPaddingTop() + getPaddingBottom() + (int) offset * lines + (int)fm.bottom;
  81. height = indeedHeight > heightSize ? heightSize : indeedHeight;
  82. } else {
  83. int lines = calculateLines(content, width - getPaddingLeft() - getPaddingRight()).size();
  84. height = getPaddingTop() + getPaddingBottom() + (int) offset * lines + (int)fm.bottom;
  85. }
  86. setMeasuredDimension(width, height);
  87. }
  88. @Override
  89. protected void onDraw(Canvas canvas) {
  90. super.onDraw(canvas);
  91. float x = getPaddingLeft();
  92. float y = -fm.top + getPaddingTop();
  93. ArrayList<String> list = calculateLines(content, getWidth() - getPaddingLeft() - getPaddingRight());
  94. for (String text : list) {
  95. canvas.drawText(text, x, y, mPaint);
  96. y += offset;
  97. }
  98. }
  99. private ArrayList<String> list = new ArrayList<>(0);
  100. private ArrayList<String> calculateLines(String content, int width) {
  101. list.clear();
  102. int length = content.length();
  103. float textWidth = mPaint.measureText(content);
  104. if (textWidth <= width) {
  105. list.add(content);
  106. return list;
  107. }
  108. int start = 0, end = 1;
  109. while (start < length) {
  110. if (mPaint.measureText(content, start, end) > width) {
  111. list.add(content.substring(start, end - 1));
  112. start = end - 1;
  113. } else if (end < length) {
  114. end++;
  115. }
  116. if (end == length) {
  117. list.add(content.subSequence(start, end).toString());
  118. break;
  119. }
  120. }
  121. return list;
  122. }
  123. public void setText(String text) {
  124. if (null == text || text.trim().length() == 0) {
  125. content = "";
  126. } else {
  127. content = text;
  128. }
  129. invalidate();
  130. }
  131. /**
  132. * @param textColor R.color.xx
  133. */
  134. public void setTextColor(@ColorRes int textColor) {
  135. mPaint.setColor(getResources().getColor(textColor));
  136. invalidate();
  137. }
  138. /**
  139. * @param textSize R.dimen.xx
  140. */
  141. public void setTextSize(@DimenRes int textSize) {
  142. mPaint.setTextSize(getResources().getDimension(textSize));
  143. initParams();
  144. invalidate();
  145. }
  146. /**
  147. * @param spacing R.dimen.xx
  148. */
  149. public void setLineSpacingExtra(@DimenRes int spacing) {
  150. this.lineSpace = getResources().getDimension(spacing);
  151. initParams();
  152. invalidate();
  153. }
  154. }

将默认的textview替换为上面的自定义view就可以解决了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/242752
推荐阅读
相关标签
  

闽ICP备14008679号