【问题标题】:adding button onto a view programatically以编程方式将按钮添加到视图上
【发布时间】:2013-07-17 08:47:06
【问题描述】:

如果我想在这个类中添加一个按钮,以便我可以调用onclicklistener,我应该怎么做?我还提供了我要添加这个视图的活动类。

活动:

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;

import android.content.Context;



public class NewGame extends Activity {

View view;
Context context;
RelativeLayout layout;
GameView gameview;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    gameview=new GameView(this);
    setContentView(gameview);
    //layout = (RelativeLayout) findViewById(R.id.relative_layout);



    //layout.addView(gameview);

}


}

视图: 公共类 GameView 扩展视图 { 路径圈; 油漆 cPaint; 油漆 tPaint; 字符串 z; int i = 65,strt,arc,leftx,topy,rightx,bottomy,maxx,maxy; 布尔标志1,标志2,标志3; 双 n1, n2; 整数 n, n3 = 180,n4,n5 = 90; 浮动 f1 = 180,f2 = 90; 按钮 b1; 随机 r = new Random(); RectF 椭圆;

    public GameView(Context context) {
        super(context);

        leftx = 0;
        topy = 60;
        rightx = 150;
        bottomy = 120;

        z = String.valueOf(Character.toChars(i));

        cPaint = new Paint();
        cPaint.setColor(Color.RED);

        strt = 45;
        arc = 315;

        n1 = Math.random() * 600;
        Log.d("random", z);

        if (flag2 == false)
            new DrawThread(this);

        // cPaint.setStrokeWidth(2);
        tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        tPaint.setColor(Color.BLACK);
        float scale = getResources().getDisplayMetrics().scaledDensity;
        tPaint.setTextSize(20 * scale);
    }

    public void onSizeChanged(int w,int h,int oldh,int oldw) {
        maxx = oldw;
        maxy = oldh;
    }

    //@Override
    protected void onDraw(Canvas canvas) {
        // Drawing commands go here
        oval = new RectF(leftx,topy,rightx,bottomy);
        canvas.drawArc(oval, strt, arc, true, cPaint);
        while (i < 90)  {
            canvas.drawText(String.valueOf(Character.toChars(i)),f1,f2, tPaint);
            break;
        }
    }
}

【问题讨论】:

    标签: android button


    【解决方案1】:

    首先,为了让您的自定义视图有一个 addView(View v) 方法,它必须扩展 ViewGroup 而不是 View;那么你可以使用这个代码

    b1=new Button(context);
    b1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT ));
    b1.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                }
            });
            this.addView(b1);
    

    【讨论】:

    • 在做了你上面提到的之后,我现在得到一个空白屏幕。你认为可能是什么问题?
    • 尝试为按钮设置一个id b1.setId(1234)
    • 还是黑屏
    • 尝试使用 b1.setText("your text") 添加文本并使用 LayoutParams.FILL_PARENT 而不是 LayoutParams.WRAP_CONTENT。如果您不将按钮的代码放入自定义视图中怎么办?您如何将自定义视图添加到布局中?在 xml 中还是通过代码?
    • 先生,我也提供了上面的活动类。
    【解决方案2】:

    你可以这样做:

    Button bt = new Button(this);
    bt.setText("A Button");
    bt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    linerLayout.addView(bt);
    

    然后你就可以这样做了

    bt.setOnClickListener(new View.OnClickListener() {
    
    //TO DO
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多