简单的自定义控件 - 强制关闭
我一直在使用eclipse.the默认的系统生成的代码看起来如下简单的自定义控件 - 强制关闭
package com.rmycustomclass.bengler;
import android.app.Activity;
import android.os.Bundle;
public class RMyCustomClassActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
我想尝试一个简单的自定义控件创建的Android项目,所以我改变了代码如下。当我搜索网络创建一个简单的自定义控件时,提到像“通过继承视图创建类”。所以通过修改代码尝试下面的代码。但它没有运行,并始终强制关闭。
package com.rmycustomclass.bengler;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class RMyCustomClassActivity extends View {
private Paint myPaint;
public RMyCustomClassActivity(Context cxt, AttributeSet attribute){
super(cxt,attribute);
init();
}
public RMyCustomClassActivity(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init();
}
public void init(){
myPaint = new Paint();
myPaint.setTextSize(12);
myPaint.setColor(0xFF668800);
myPaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawText("TEEEST", 100, 100, myPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
this.setMeasuredDimension(150,200);
}
}
下面是我的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="Enter 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:layout_weight="1">
</Button>
<Button android:layout_width="wrap_content" android:id="@+id/button1" android:layout_height="40dip" android:text="My button" android:layout_weight="1"></Button>
<test.control
android:id="@+id/control"
android:layout_height="match_parent"> </test.control>
</TableLayout>
</LinearLayout>
它连接你的活动(RMyCustomClassActivity你的情况)作为Android清单中的启动程序...但是因为您已将活动更改为视图 android运行时无法找到要启动的活动...查看您的清单文件a nd你会发现你的班级作为发射器活动
你能告诉我应该改变什么 – user198725878
对于你的自定义视图,创建一个不同的类并在你的活动/布局中使用这个视图来测试它 – anz
添加机器人:当您在Eclipse中创建一个项目layout_width = “match_parent”
<test.control
android:id="@+id/control"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
发布您的LogCat错误,以便我们可以看到发生了什么。 – Sam
您的View类与您的Activity类名称相同,您应该为View使用更具描述性的名称......这可能很容易导致冲突。 – Sam