Я создал свой собственный TextView, расширив класс TextView, чтобы улучшить его отображение. Я создал различные Paint и прочее, чтобы добавить некоторый запас. Затем текст должен отображаться сразу после поля. Если установитьПроблема с шириной TextView при создании моего собственного TextView
android:layout_width="fill_parent"
дисплей нормально, и моя линия полностью заполнена с белым фоном (как определено в моем макете). НО если я установил
android:layout_width="wrap_content"
дисплей идет не так, и конец текста моего TextView обрезается. Я предполагаю, что это связано с тем, что я сделал Translate в методе onDraw своего TextView, но я не знаю, как его исправить. Обратите внимание, что мне нужно установить wrap_content, потому что я хочу добавить еще один TextBox сразу после него и LinearLayout вокруг обоих, но на данный момент другой TextBox стирает часть содержимого первой.
код моего нового TextBox заключается в следующем:
package com.flo.ui;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.TextView;
import com.flo.musicalnotes.R;
public class NoteItemTextView extends TextView {
// Properties
private Paint marginPaint;
private Paint linePaint;
private Paint circlePaint;
private int paperColor;
private float margin;
private float marginEnd;
private float textStart;
// Initialization
public NoteItemTextView(Context context) {
super(context);
this.Init(context);
}
public NoteItemTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
this.Init(context);
}
private void Init(Context context)
{
// Resources retrieval
Resources myResources = getResources();
// Brush definition
this.marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
this.marginPaint.setColor(myResources.getColor(R.color.marginColor));
this.marginPaint.setStrokeWidth((float) 1.8);
this.linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
this.linePaint.setColor(myResources.getColor(R.color.underlineColor));
this.circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
this.circlePaint.setColor(myResources.getColor(R.color.marginColor));
this.circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
// various resources
this.paperColor = myResources.getColor(R.color.bgColor);
this.margin = myResources.getDimension(R.dimen.marginSize);
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int ot = getResources().getConfiguration().orientation;
switch(ot)
{
case Configuration.ORIENTATION_LANDSCAPE:
this.marginEnd = this.margin + metrics.widthPixels/100;
this.textStart = this.marginEnd + metrics.widthPixels/100;
case Configuration.ORIENTATION_PORTRAIT:
this.marginEnd = this.margin + metrics.heightPixels/100;
this.textStart = this.marginEnd + metrics.heightPixels/100;
default:
this.marginEnd = this.margin + 5;
this.textStart = this.marginEnd + 10;
}
}
//@Override
protected void onDraw(Canvas canvas) {
// paper color
canvas.drawColor(this.paperColor);
// lines drawing
canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), this.linePaint);
// marge drawing
canvas.drawLine(this.margin, 0, this.margin, getMeasuredHeight(), this.marginPaint);
canvas.drawLine(this.marginEnd, 0, this.marginEnd, getMeasuredHeight(), this.marginPaint);
double x = (this.textStart + this.marginEnd)/1.8;
float y1 = getMeasuredHeight()/3;
float y2 = getMeasuredHeight() * 2/3;
float radius = (float) 2.5;
canvas.drawCircle((float) x, y1, radius, this.circlePaint);
canvas.drawCircle((float) x, y2, radius, this.circlePaint);
canvas.save();
canvas.translate(this.textStart, 0);
super.onDraw(canvas);
canvas.restore();
}
}
Спасибо за вашу помощь!
вы проверили мой ответ? – Krish
Нажмите галочку (галочку) слева под стрелками для голосования, чтобы принять ответ. – Krish