尝试创建自定义文本视图但显示错误
Trying to create custom textview but showing error
这是我的 class test.java
代码
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);
}
}
main.xml
<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>
Logcat :
致命异常:主要
java.lang.RuntimeException: 无法启动 activity ComponentInfo{com.example.testdroid/com.example.testdroid.MainActivity}: android.view.InflateException: 二进制 XML 文件行 # 25: 膨胀错误 class com.example.testdroid.test
为文本视图创建一个 class,如下所示:-
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);
}
}
}
这是我的 class test.java
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);
}
}
main.xml
<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>
Logcat :
致命异常:主要 java.lang.RuntimeException: 无法启动 activity ComponentInfo{com.example.testdroid/com.example.testdroid.MainActivity}: android.view.InflateException: 二进制 XML 文件行 # 25: 膨胀错误 class com.example.testdroid.test
为文本视图创建一个 class,如下所示:-
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);
}
}
}