在网上看的自制的textview,但右边对不齐,而且字体大小在不同大小的屏幕上显示也不一样,求指教。
自制的textviewpackage 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);
}
}应用: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("对 name; phone;type; date;duration;的封装,普通充值,充50元到账75元,充100元到账 200元,所有话费一次到账!");
view.setMovementMethod(ScrollingMovementMethod.getInstance());
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:textSize="30" />
</ScrollView>
</LinearLayout>
自制的textviewpackage 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);
}
}应用: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("对 name; phone;type; date;duration;的封装,普通充值,充50元到账75元,充100元到账 200元,所有话费一次到账!");
view.setMovementMethod(ScrollingMovementMethod.getInstance());
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:textSize="30" />
</ScrollView>
</LinearLayout>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货