android - 在 Android 中向自定义 View 添加按钮

标签 android button custom-view

我想为这个自定义 View 添加一个按钮。 此 View 通过显示

setContentView(mDrawView);

我试过用

mDrawView.addView(mButton);

但我的 View 没有实现 addView() 函数。 有没有办法添加按钮?这是我的绘图 View

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

}

}
}

我也不知道如何将此 View 附加到 xml。使用 XML 而不是 setContentView(mDrawView) 更好吗? ?

最佳答案

您只能将 View 添加到 ViewGroup。因此需要扩展一个 View 组,如 LinearLayout、RelativeLayout 或 ViewGroup。然后您将能够向其添加按钮。

关于android - 在 Android 中向自定义 View 添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9204625/

相关文章:

android - 当我的移动数据连接关闭时,如何在 SignalR 中断开连接

android - GMail API 在 UserRecoverableAuthUIException 之前授予权限

android - 检查确切按钮/texview/上的动画是否已经结束

ios - 点击按钮UICollectionViewCell宽高变化

ios - 自定义 View 很慢

android - 如何在代码中获取在 attrs.xml 中创建的枚举

javascript - Android - Webview(kitkat 及以下): Error In Java Script Function 'includes'

android - android软键盘上方需要Prev,Next按钮

xcode - uinavigationbar 中的自定义背景非应用程序范围,每个 View 都不同

java - Eclipse 启动失败谷歌插件