java - TextView 未出现在 onDraw 对象中

标签 java android xml android-canvas

我试图让 TextView “2”(黑色)出现在我的 View 中心,但由于某种原因它不会出现。有谁知道这里出了什么问题以及如何解决此错误?有没有办法以编程方式而不是使用 XML 来显示它并更改它的颜色?

XML

<com.apptacularapps.car.RectangleTextView
    android:layout_width="120dp"
    android:layout_height="40dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:text="2"
    android:gravity="center"
    android:background="#808080"/>

Java

public class RectangleTextView extends View {
    Paint paint;
    private TextPaint mTextPaint;


    public RectangleTextView(Context context) {
        super(context);
        init();
    }

    public RectangleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RectangleTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(1); // convert to dp?
        paint.setStyle(Paint.Style.STROKE); // delete line for filled rect

        mTextPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setColor(Color.BLACK);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawText("2", 0, 0, paint);
        paint.setTextSize(20);

        int w = canvas.getWidth();
        int h = canvas.getHeight();

        int rectWidth = w/5;
        int space = w/15;
        int topRectHeight = getPaddingTop();
        int bottomRectHeight = getPaddingBottom();

        for (int i = 0; i < 4; i++) {
            int left = i * (rectWidth + space);
            int right = left + rectWidth;

            Rect rect = new Rect(left, 0, right, topRectHeight);
            canvas.drawRect(rect, paint);

            Rect rect2 = new Rect(left, h - bottomRectHeight, right, h);
            canvas.drawRect(rect2, paint);
        }
    }
}

enter image description here

最佳答案

您没有定义文本绘制方式的绘画对象,也没有在 onDraw() 方法中调用 canvas.drawText()。这样做:

    private TextPaint mTextPaint;

    private void init() {
    mTextPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
    // You can tweak the appearance of the textpaint here
    mTextPaint.setTextAlign(Align.CENTER);
    mTextPaint.setColor(color);
    }

    @Override
    public void onDraw() {
    super.onDraw()
    // You can tweak the positioning of the text here
    canvas.drawText("2", 25, 25, mTextPaint);
    }

关于java - TextView 未出现在 onDraw 对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31997135/

相关文章:

java - Parcelable 对象错误 : Unmarshalling unknown type code *** at offset ***

Android 权限和 guid 和 uid

android - 将微调器中的数据插入数据库

java - 使用 Firebase 登录后如何获取 Twitter 用户的用户名。安卓

java - 我想通过解码将 XML 转换为 Java

java - 使用 DOM 解析器从 XML 文档创建 JTree

java - 是否可以在 Spring Boot 中读取响应的正文?

用于字节操作的 Java RandomAccessFile 与 DataInputStream

java - 不改变时区的日期时间解析

java - 从 jaxb 生成的 xml 中删除独立的 ="yes"