尝试创建自定义textview但显示错误

时间:2015-06-15 12:36:10

标签: android textview

以下是我的课程package com.example.testdroid; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.widget.TextView; public class test extends TextView{ public test(Context context) { super(context); // TODO Auto-generated constructor stub this.setText("Hello World"); this.setTextSize(20f); this.setTextColor(Color.RED); } protected void onDraw (Canvas canvas) { super.onDraw(canvas); } }

的代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

 <com.example.testdroid.test
        android:id="@+id/test1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignRight="@+id/button1"
        android:layout_marginBottom="68dp"
       />
</RelativeLayout>

main.xml中

def check_block( self, (x, y) ):

    """
    Check if the x, y coordinate can have a block placed there.
    That is; if there is a 'landed' block there or it is outside the
    board boundary, then return False, otherwise return true.
    """
    if x < 0 or x >= self.max_x or y < 0 or y >= self.max_y:
        return False
    elif self.landed.has_key( (x, y) ):
        return False
    else:
        return True

Logcat:

致命的例外:主要 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.testdroid / com.example.testdroid.MainActivity}:android.view.InflateException:二进制XML文件行#25:错误膨胀类com.example.testdroid.test

1 个答案:

答案 0 :(得分:1)

为textview创建一个类,如下所示: -

public class TextViews extends TextView{

    public TextViews (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

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

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

    private void init() {
        if (!isInEditMode()) {
            this.setText("Hello World");
            this.setTextSize(20f);
            this.setTextColor(Color.RED);

        }
    }
}