如何在布局中使用我自己的视图?

时间:2011-11-23 07:37:41

标签: android

我创建了一个视图,视图代码是,

   public class MyDraw extends View{



    //List<Point> mArryLstpoints = new ArrayList<Point>();
    Paint paint =new Paint();
    Paint mPaintAlphabet = new Paint();
    public MyDraw(Context context) {
        super(context);


        paint.setColor(Color.BLUE);
        paint.setAntiAlias(true);


        // TODO Auto-generated constructor stub
    }
    public MyDraw(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);

        paint.setColor(Color.BLUE);
        paint.setAntiAlias(true);

        mPaintAlphabet.setDither(true);
        mPaintAlphabet.setColor(0xFFFF0000);
        mPaintAlphabet.setStyle(Paint.Style.STROKE);
        mPaintAlphabet.setStrokeJoin(Paint.Join.ROUND);
        mPaintAlphabet.setStrokeCap(Paint.Cap.ROUND);
        mPaintAlphabet.setStrokeWidth(3);
        mPaintAlphabet.setTextSize(400);
    }

    public void onDraw(Canvas canvas){
        canvas.drawColor(Color.GRAY);
        canvas.drawText("A",100,350, mPaintAlphabet);
        System.out.println("in on draw");
        for(Point mPoint:MyAlphabetsActivity.mArryLstpoints)
        canvas.drawCircle(mPoint.x, mPoint.y, 12, paint);

    }

    }

我想在布局中使用此视图,我想为视图设置背景图像。我正在使用以下代码,

  <?xml version="1.0" encoding="utf-8"?>

  <com.qteq.myalphabets.MyDraw
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/android:list"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" ></com.qteq.myalphabets.MyDraw>

我的活动课是,

public class MyAlphabetsActivity extends Activity {
    /** Called when the activity is first created. */

    MyDraw mMyDraw;
    Button mBtnOk;
    AttributeSet attributeSet;
    public static List<Point> mArryLstpoints = new ArrayList<Point>();
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /*
         * getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
         * WindowManager.LayoutParams.FLAG_FULLSCREEN);
         */
        /*
         * mMyDraw=new MyDraw(this); setContentView(mMyDraw);
         * mMyDraw.requestFocus();
         */
        mMyDraw = (MyDraw) findViewById(R.id.mMyDraw_layout);
        mMyDraw.bringToFront();
        mMyDraw.requestFocus();
        mMyDraw.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                Point mPoint=new Point();
                mPoint.x=event.getX();
                mPoint.y=event.getY();
                System.out.println("in on touch");
                mArryLstpoints.add(mPoint);
                System.out.println("Array list is-----"+mArryLstpoints);

                setContentView(R.layout.main);
                return true;
            }
        });
        // setContentView(R.layout.main);
    }
}
    class Point{
        public float srtx;
        float x, y;

        @Override
        public String toString() {
            System.out.println("in on point");
            return x + ", " + y;       
    }
    }

和显示和onTouch方法只调用一次.....请帮帮我

2 个答案:

答案 0 :(得分:1)

您可以将视图放在XML中的布局中,并且在该布局中也可以有其他元素。您也可以将布局背景设置为某种东西。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/android:list"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" 
       android:orientation="vertical"
       android:background="@drawable/mybackground">

 <com.qteq.myalphabets.MyDraw
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"/>

 <Button
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="Button" />


</LinearLayout>

答案 1 :(得分:1)

您正在使用fill_parent宽度和高度添加视图,因此没有空间可以添加其他视图,如按钮和图像。

可能的实施之一

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/android:list"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">

  <com.qteq.myalphabets.MyDraw
   xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/android:list"
       android:layout_width="fill_parent"
       android:layout_height="wrap_contentt" ></com.qteq.myalphabets.MyDraw>
<Button/>
<ImageView/>
</LinearLayout>

别忘了添加构造函数

public MyDraw(Context context, AttributeSet attributeSet) {
        super(context, ttributeSet);
MyDraw.java中的

** * ** * ** * 修改 * * ** * **

onTouch()中的

返回super(v, event)而不是true; 还要检查是否需要将setOnTouchListener()写入视图或画布。