2013-08-29 38 views

回答

0

您不必使用XML。它只是eclipse希望你创建UI的默认方式。做你想做的最好的方法是创建一个扩展视图并覆盖ondraw方法的类。 然后你调用setContentView(yourclass);

下面是一些简单的在屏幕上绘制线条的代码示例,该线条应该足以让您开始。

主要活动:

package com.example.myexample; 

import android.os.Bundle; 
import android.app.Activity; 


public class MainActivity extends Activity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    //Create class that extends view 
    Example myexample = new Example(this); 

    setContentView(myexample); 
} 

}

和示例类是这样的:

package com.example.myexample; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.view.View; 

public class Example extends View{ 

    //You will need to declare a variable of paint to draw 
    Paint mypaint = new Paint(); 


    //constructor 
    public Example(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    //This is the function where you draw to the screen 
    @Override 
    public void onDraw (Canvas canvas){ 


     canvas.drawLine(25, 25, 25, 50, mypaint); 
    } 

}