创建自己的TextView时TextView宽度问题

时间:2015-08-10 18:16:52

标签: android android-layout textview

我通过扩展TextView类来创建自己的TextView,以改善其显示。我创建了各种Paint和东西来添加一种边距。然后文本必须在边距之后显示。如果我设置

android:layout_width="fill_parent"

显示正常,我的线条完全充满白色背景(在我的布局中定义)。 但是如果我设置

android:layout_width="wrap_content"

显示错误,我的TextView文本末尾被裁剪。我想这是因为我在TextView的onDraw方法中创建了一个Translate,但我不知道如何修复它。 请注意,我需要set wrap_content,因为我想要在其后添加另一个TextBox,并在两者周围添加LinearLayout,但目前其他TextBox会删除第一个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();   
}
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

尝试将此代码添加到自定义textview类

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int height = getMeasuredHeight();
        int width = getMeasuredWidth();
        Log.e(getClass().getSimpleName() , String.format("height x %s ::: width x %s ",height , width));
        float density = getResources().getDisplayMetrics().density;
        //Extra space after last letter.
        float px = 2 * density;
        int adjustedWidth = (int) (width + textStart + px);
        setMeasuredDimension(adjustedWidth, height);

    }

答案 1 :(得分:0)

将此添加到您的textview

android:paddingRight="25dp"