当前位置:   article > 正文

android 自定义textView,实现排版对齐和换行_android textview 多行 左对齐

android textview 多行 左对齐

android开发中的textview可以自动换行,但是对于显示纯英文文字来说很好用,如果夹杂了中文字符后,全角字符和半角字符混在一块儿,就会出现文字排版参差不齐,超级难看,这就需要重写textview来实现我们需要的显示方式。

下面贴上我的代码:

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:ae="http://www.angellecho.com/"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="@android:color/black"
  7. android:gravity="center_horizontal"
  8. android:orientation="vertical" >
  9. <ScrollView
  10. android:layout_width="wrap_content"
  11. android:layout_height="200dp" >
  12. <com.wigit.MyTextView2
  13. android:id="@+id/view"
  14. android:layout_width="fill_parent"
  15. android:layout_height="200dp"
  16. android:layout_marginTop="20dp"
  17. android:layout_marginLeft="20dp"
  18. android:layout_marginRight="20dp"
  19. android:background="@android:color/darker_gray"
  20. ae:marginLeft="20"
  21. ae:marginRight="20"
  22. ae:paddingLeft="20"
  23. ae:paddingRight="20"
  24. ae:textColor="#FFFFFF"
  25. ae:textSize="30" />
  26. </ScrollView>
  27. </LinearLayout>
重写后的textview如下,可能与网上其他例子有些相似,但是还解决了drawText不能换行的问题:

  1. package com.wigit;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.util.AttributeSet;
  8. import android.widget.TextView;
  9. public class MyTextView2 extends TextView{
  10. private final String namespace = "http://www.angellecho.com/";
  11. private String text;
  12. private float textSize;
  13. private float paddingLeft;
  14. private float paddingRight;
  15. private float marginLeft;
  16. private float marginRight;
  17. private int textColor;
  18. private Paint paint1 = new Paint();
  19. private float textShowWidth;
  20. public MyTextView2(Context context, AttributeSet attrs) {
  21. super(context, attrs);
  22. text = attrs.getAttributeValue(
  23. "http://schemas.android.com/apk/res/android", "text");
  24. textSize = attrs.getAttributeIntValue(namespace, "textSize", 15);
  25. textColor = attrs.getAttributeIntValue(namespace, "textColor",Color.WHITE);
  26. paddingLeft = attrs.getAttributeIntValue(namespace, "paddingLeft", 0);
  27. paddingRight = attrs.getAttributeIntValue(namespace, "paddingRight", 0);
  28. marginLeft = attrs.getAttributeIntValue(namespace, "marginLeft", 0);
  29. marginRight = attrs.getAttributeIntValue(namespace, "marginRight", 0);
  30. paint1.setTextSize(textSize);
  31. paint1.setColor(textColor);
  32. paint1.setAntiAlias(true);
  33. textShowWidth = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth() - paddingLeft - paddingRight - marginLeft - marginRight;
  34. }
  35. @Override
  36. protected void onDraw(Canvas canvas) {
  37. //super.onDraw(canvas);
  38. int lineCount = 0;
  39. text = this.getText().toString();//.replaceAll("\n", "\r\n");
  40. if(text==null)return;
  41. char[] textCharArray = text.toCharArray();
  42. // 已绘的宽度
  43. float drawedWidth = 0;
  44. float charWidth;
  45. for (int i = 0; i < textCharArray.length; i++) {
  46. charWidth = paint1.measureText(textCharArray, i, 1);
  47. if(textCharArray[i]=='\n'){
  48. lineCount++;
  49. drawedWidth = 0;
  50. continue;
  51. }
  52. if (textShowWidth - drawedWidth < charWidth) {
  53. lineCount++;
  54. drawedWidth = 0;
  55. }
  56. canvas.drawText(textCharArray, i, 1, paddingLeft + drawedWidth,
  57. (lineCount + 1) * textSize, paint1);
  58. drawedWidth += charWidth;
  59. }
  60. setHeight((lineCount + 1) * (int) textSize + 5);
  61. }
  62. }

测试activity
  1. package com.text;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import com.wigit.MyTextView2;
  7. import android.app.Activity;
  8. import android.content.Context;
  9. import android.content.res.AssetManager;
  10. import android.os.Bundle;
  11. import android.text.method.ScrollingMovementMethod;
  12. public class MTextViewActivity extends Activity {
  13. MyTextView2 view;
  14. /** Called when the activity is first created. */
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main1);
  19. view = (MyTextView2) findViewById(R.id.view);
  20. view.setText(getAssetsString(this,"1.txt"));
  21. view.setMovementMethod(ScrollingMovementMethod.getInstance());
  22. }
  1. //读取assets文件夹下文本字符串
  2. public String getAssetsString(Context context,String fileName){
  3. StringBuffer sb = new StringBuffer();
  4. try {
  5. AssetManager am = context.getAssets();
  6. InputStream in = am.open(fileName);
  7. BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  8. String line;
  9. while((line = reader.readLine())!=null){
  10. line += ("\n");
  11. sb.append(line);
  12. }
  13. reader.close();
  14. in.close();
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. return sb.toString();
  19. }
  20. }

并附上例子如下(免积分哦):
http://download.csdn.net/detail/dyc333236081818/4231656

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

闽ICP备14008679号