赞
踩
android开发中的textview可以自动换行,但是对于显示纯英文文字来说很好用,如果夹杂了中文字符后,全角字符和半角字符混在一块儿,就会出现文字排版参差不齐,超级难看,这就需要重写textview来实现我们需要的显示方式。
下面贴上我的代码:
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:ae="http://www.angellecho.com/"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@android:color/black"
- android:gravity="center_horizontal"
- android:orientation="vertical" >
-
- <ScrollView
- android:layout_width="wrap_content"
- android:layout_height="200dp" >
- <com.wigit.MyTextView2
- android:id="@+id/view"
- android:layout_width="fill_parent"
- android:layout_height="200dp"
- android:layout_marginTop="20dp"
- android:layout_marginLeft="20dp"
- android:layout_marginRight="20dp"
- android:background="@android:color/darker_gray"
- ae:marginLeft="20"
- ae:marginRight="20"
- ae:paddingLeft="20"
- ae:paddingRight="20"
- ae:textColor="#FFFFFF"
- ae:textSize="30" />
- </ScrollView>
- </LinearLayout>
重写后的textview如下,可能与网上其他例子有些相似,但是还解决了drawText不能换行的问题:
- package com.wigit;
-
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.util.AttributeSet;
- import android.widget.TextView;
-
- public class MyTextView2 extends TextView{
- private final String namespace = "http://www.angellecho.com/";
- private String text;
- private float textSize;
- private float paddingLeft;
- private float paddingRight;
- private float marginLeft;
- private float marginRight;
- private int textColor;
-
-
- private Paint paint1 = new Paint();
- private float textShowWidth;
- public MyTextView2(Context context, AttributeSet attrs) {
- super(context, attrs);
- text = attrs.getAttributeValue(
- "http://schemas.android.com/apk/res/android", "text");
- textSize = attrs.getAttributeIntValue(namespace, "textSize", 15);
- textColor = attrs.getAttributeIntValue(namespace, "textColor",Color.WHITE);
- paddingLeft = attrs.getAttributeIntValue(namespace, "paddingLeft", 0);
- paddingRight = attrs.getAttributeIntValue(namespace, "paddingRight", 0);
- marginLeft = attrs.getAttributeIntValue(namespace, "marginLeft", 0);
- marginRight = attrs.getAttributeIntValue(namespace, "marginRight", 0);
- paint1.setTextSize(textSize);
- paint1.setColor(textColor);
- paint1.setAntiAlias(true);
- textShowWidth = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth() - paddingLeft - paddingRight - marginLeft - marginRight;
- }
- @Override
- protected void onDraw(Canvas canvas) {
- //super.onDraw(canvas);
- int lineCount = 0;
- text = this.getText().toString();//.replaceAll("\n", "\r\n");
- if(text==null)return;
- char[] textCharArray = text.toCharArray();
- // 已绘的宽度
- float drawedWidth = 0;
- float charWidth;
- for (int i = 0; i < textCharArray.length; i++) {
- charWidth = paint1.measureText(textCharArray, i, 1);
-
- if(textCharArray[i]=='\n'){
- lineCount++;
- drawedWidth = 0;
- continue;
- }
- if (textShowWidth - drawedWidth < charWidth) {
- lineCount++;
- drawedWidth = 0;
- }
- canvas.drawText(textCharArray, i, 1, paddingLeft + drawedWidth,
- (lineCount + 1) * textSize, paint1);
- drawedWidth += charWidth;
- }
- setHeight((lineCount + 1) * (int) textSize + 5);
- }
- }
- package com.text;
-
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
-
- import com.wigit.MyTextView2;
-
- import android.app.Activity;
- import android.content.Context;
- import android.content.res.AssetManager;
- import android.os.Bundle;
- import android.text.method.ScrollingMovementMethod;
-
- public class MTextViewActivity extends Activity {
- MyTextView2 view;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main1);
-
- view = (MyTextView2) findViewById(R.id.view);
- view.setText(getAssetsString(this,"1.txt"));
- view.setMovementMethod(ScrollingMovementMethod.getInstance());
- }
- //读取assets文件夹下文本字符串
- public String getAssetsString(Context context,String fileName){
- StringBuffer sb = new StringBuffer();
- try {
- AssetManager am = context.getAssets();
- InputStream in = am.open(fileName);
- BufferedReader reader = new BufferedReader(new InputStreamReader(in));
- String line;
- while((line = reader.readLine())!=null){
- line += ("\n");
- sb.append(line);
- }
- reader.close();
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return sb.toString();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。