在Android中向自定义视图添加按钮
我想为这个自定义视图添加一个按钮。 此视图通过以下方式显示:在Android中向自定义视图添加按钮,android,button,custom-view,Android,Button,Custom View,我想为这个自定义视图添加一个按钮。 此视图通过以下方式显示: setContentView(mDrawView); 我试着用 mDrawView.addView(mButton); 但是我的视图没有实现addView()函数。 有没有办法添加按钮?这是我的图纸 package com.android.connect4; import android.content.Context; import android.graphics.Canvas; import android.graphic
setContentView(mDrawView);
我试着用
mDrawView.addView(mButton);
但是我的视图没有实现addView()函数。
有没有办法添加按钮?这是我的图纸
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
}
}
}
package com.android.connect4;
导入android.content.Context;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.util.AttributeSet;
导入android.util.Log;
导入android.view.MotionEvent;
导入android.view.view;
导入android.view.view.OnTouchListener;
公共类DrawView扩展了视图实现OnTouchListener{
专用静态最终整型框尺寸=35;
私人油漆;
私人国际流动;
私人国际mCol;
私营机构;
私有博弈模型;
公共绘图视图(上下文上下文、int vHeight、int vWidth、GameModel vGameModel){
超级(上下文);
//TODO自动生成的构造函数存根
设置聚焦(真);
setFocusableInTouchMode(真);
mPaint=新油漆();
mGameModel=vGameModel;
mPaint.setAntiAlias(true);
mHeight=vHeight;
mRow=GameModel.ROWS;
mCol=GameModel.COLUMNS;
init();
setOnTouchListener(这个);
mGameModel.attach(此);
}
私有void init(){
//TODO自动生成的方法存根
mGameModel.UserFunction('I');
}
@凌驾
公共空白onDraw(画布){
对于(int vCount=mRow-1;vCount>=0;vCount--)
对于(int hCount=0;hCount您只能将视图添加到视图组。因此,您需要扩展一个视图组,如LinearLayout、RelativeLayout或ViewGroup。然后您就可以向其中添加按钮。我真的需要扩展一个布局类吗?我可以实例化其中的任何一个并在其中添加此视图+一个按钮吗反线性布局并在里面添加按钮。谢谢老兄!