Android 简单自定义类扩展视图
我想创建一个自定义视图类。但我运行应用程序时出错 这是我的班级:Android 简单自定义类扩展视图,android,class,view,extends,Android,Class,View,Extends,我想创建一个自定义视图类。但我运行应用程序时出错 这是我的班级: package test.start; import android.util.AttributeSet; import android.view.*; import android.graphics.*; import android.content.Context; public class control extends View{ private Paint paint; public control
package test.start;
import android.util.AttributeSet;
import android.view.*;
import android.graphics.*;
import android.content.Context;
public class control extends View{
private Paint paint;
public control(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
init();
}
public void init(){
paint = new Paint();
paint.setTextSize(12);
paint.setColor(0xFF668800);
paint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawText("TEEEST", 100, 100, paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
this.setMeasuredDimension(150,200);
}
}
还有main.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:orientation="vertical">
<TextView android:id="@+id/textView1" android:text="@string/text" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tableLayout1">
<Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:background="@drawable/custom_button" android:layout_weight="1"></Button>
<Button android:layout_width="wrap_content" android:id="@+id/button1" android:layout_height="40dip" android:text="@string/button" android:layout_weight="1"></Button>
<test.control
android:id="@+id/control"
android:layout_width="match_parent"
android:layout_height="match_parent"> </test.control>
</TableLayout>
</LinearLayout>
错误消息:
无法启动活动
ComponentInfo{de.me.start/test.start.StartActivity}:
android.view.InflateException:二进制
XML文件行#10:错误膨胀
类test.start.control
大宗报价
但是我可以在图形布局中查看控件。您应该用以下方式在XML中声明它:
<view class="de.test.start.control"
android:id="@+id/control"
android:layout_width="match_parent"
android:layout_height="match_parent"> </view>
您应该以以下方式在XML中声明它:
<view class="de.test.start.control"
android:id="@+id/control"
android:layout_width="match_parent"
android:layout_height="match_parent"> </view>
尝试提供另一个版本的构造函数:
public control(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
尝试提供其他版本的构造函数:
public control(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
是的。是否有任何预定义类extends view的示例项目?@Passy Hi Passy,您是否找到您搜索的任何示例项目?。。。如果有,请与我们分享。谢谢你。是否有任何预定义类extends view的示例项目?@Passy Hi Passy,您是否找到您搜索的任何示例项目?。。。如果有,请与我们分享。谢谢