2012-02-09 4 views
1

이 사용자 정의보기에 버튼을 추가하고 싶습니다. 이보기 내가 안드로이드에서 사용자 정의보기에 버튼 추가하기

mDrawView.addView(mButton); 

를 사용하여 시도

setContentView(mDrawView); 

을 통해 표시되지만 내보기는 addView() 함수를 구현하지 않습니다. 버튼을 추가 할 수있는 방법이 있습니까? 여기 내 drawview입니다

package com.android.connect4; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 

public class DrawView extends View implements OnTouchListener { 

private static final int BOX_SIZE = 35; 
private Paint mPaint; 
private int mRow; 
private int mCol; 
private int mHeight; 
private GameModel mGameModel; 



public DrawView(Context context, int vHeight, int vWidth, GameModel vGameModel) { 
    super(context); 
    // TODO Auto-generated constructor stub 
    setFocusable(true); 
    setFocusableInTouchMode(true); 

    mPaint = new Paint(); 
    mGameModel = vGameModel; 
    mPaint.setAntiAlias(true); 

    mHeight = vHeight; 

    mRow = GameModel.ROWS; 
    mCol = GameModel.COLUMNS; 
    init(); 
    setOnTouchListener(this); 
    mGameModel.attach(this); 
} 

private void init() { 
    // TODO Auto-generated method stub 
    mGameModel.UserFunction('I'); 
} 

@Override 
public void onDraw(Canvas canvas) { 

    for(int vCount = mRow-1; vCount>=0; vCount--) 
     for(int hCount = 0; hCount<mCol; hCount++) { 
      switch(mGameModel.getCellState(vCount, hCount)) { 
      case GameModel.RED: 
       mPaint.setColor(Color.RED); 
       canvas.drawCircle(hCount*BOX_SIZE+BOX_SIZE/2, mHeight-3*BOX_SIZE-vCount*BOX_SIZE+BOX_SIZE/2, BOX_SIZE/2-5, mPaint); 
       break; 
      case GameModel.BLUE: 
       mPaint.setColor(Color.BLUE); 
       canvas.drawRect(hCount*BOX_SIZE+5, mHeight-3*BOX_SIZE-vCount*BOX_SIZE+5, hCount*BOX_SIZE+BOX_SIZE-5, mHeight-3*BOX_SIZE-vCount*BOX_SIZE+BOX_SIZE-5, mPaint); 
       break; 
      case GameModel.EMPTY_CELL: 
       mPaint.setColor(Color.WHITE); 
       canvas.drawRect(hCount*BOX_SIZE+5, mHeight-3*BOX_SIZE-vCount*BOX_SIZE+5, hCount*BOX_SIZE+BOX_SIZE-5, mHeight-3*BOX_SIZE-vCount*BOX_SIZE+BOX_SIZE-5, mPaint); 
      default: 
       break; 
      } 
     } 
    mPaint.setColor(Color.WHITE); 
    mPaint.setTextSize(20); 
    String vShow = ""; 
    switch(mGameModel.getWinner()) { 
    case GameModel.RED: 
     vShow = "Red Wins!"; 
     break; 
    case GameModel.BLUE: 
     vShow = "Blue Wins!"; 
     break; 
    default: 
     switch(mGameModel.getCurrentPlayer()) { 
     case GameModel.RED: 
      vShow = "Current Player is Red"; 
      break; 
     case GameModel.BLUE: 
      vShow = "Current Player is Blue"; 
      break; 
     default: 
      break; 
     } 
    } 
    canvas.drawText(vShow, BOX_SIZE/2, (mRow+1)*BOX_SIZE, mPaint); 
} 

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

public DrawView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    // TODO Auto-generated constructor stub 
} 

public boolean onTouch(View arg0, MotionEvent arg1) { 
    if(arg1.getActionMasked() == MotionEvent.ACTION_DOWN) { 
    char vChar = (char) ((arg1.getRawX()/BOX_SIZE)+65); 
     mGameModel.UserFunction(vChar); 
     Log.i("DrawView", (int)arg1.getRawX()/BOX_SIZE+","+(int)(mHeight-arg1.getRawY())/BOX_SIZE); 
//  Log.i("DrawView Char", Character.toString((char) (arg1.getRawX()/BOX_SIZE+65))); 
    } 
    return true; 
} 

@Override 
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) { 
    // TODO Auto-generated method stub 

} 

} 
} 

나는이 뷰를 XML에 첨부하는 방법도 모른다. setContentView (mDrawView) 대신 XML을 사용하는 것이 더 낫습니다. ?

답변

2

ViewGroup에만보기를 추가 할 수 있습니다. 따라서 LinearLayout, RelativeLayout 또는 ViewGroup과 같은 뷰 그룹을 확장해야합니다. 그런 다음 버튼을 추가 할 수 있습니다.

+0

정말 레이아웃 클래스 중 하나를 확장해야합니까? 그 중 하나를 인스턴스화하고이보기 + 버튼을 안에 추가 할 수 있습니까? –

+0

LinearLayout을 인스턴스화하고 내부에 버튼을 추가하여 직접 알아 냈습니다. 고마워 친구! –

관련 문제