这是代码的java部分
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
class surf extends View {
public surf(Context c,AttributeSet as) {
super(c,as);
}
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.MAGENTA);
canvas.drawLine(20, 20,200, 20,p);
}
}
}
没有其他视图的资源文件
<view
class="tictac.Tictac.MainActivity$surf"
android:id="@+id/graphics"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
它一打开就出现错误,我一直关注Android网站上的教程
答案 0 :(得分:0)
试试这段代码。在这里,我将自己的视图添加为主视图。
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new surf(this));
}
class surf extends View {
public surf(Context c)
{
super(c);
}
public surf(Context c,AttributeSet as) {
super(c,as);
}
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.MAGENTA);
canvas.drawLine(20, 20,200, 20,p);
}
}
}
或者您可以在XML中添加视图
<com.my.package.MyClass
android:id="@+id/graphics"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>