将视图添加到活动中

时间:2013-07-18 08:04:30

标签: android

如果我想在此课程中添加一个按钮,以便我可以调用onclicklistener,我应该怎么做?

public class GameView extends View {
    Path circle;
    Paint cPaint;
    Paint tPaint;
    String z;
    int i = 65, strt, arc, leftx, topy, rightx, bottomy, maxx, maxy;
    boolean flag1, flag2, flag3;
    double n1, n2;
    int n, n3 = 180,n4,n5 = 90;
    float f1 = 180, f2 = 90;
    Button b1;
    Random r = new Random();
    RectF  oval;

    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;
        }
    }
}

4 个答案:

答案 0 :(得分:2)

RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout);

添加以上setContentView

setContentView(R.layout.activity_new_game);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout);

您需要使用setContentView设置布局,然后初始化视图。

请注意,您可以findViewById设置为活动的当前视图层次结构

编辑:

示例:这是一个示例。如果你做类似的话,我不明白为什么它不会起作用。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".NewGame" 
android:id="@+id/relative_layout"
>

<Button
    android:id="@+id/prev_button"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/next_button"
    android:layout_alignBottom="@+id/next_button"
    android:layout_centerHorizontal="true"
    android:text="stop_label" />

  <Button
    android:id="@+id/next_button"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginRight="20dp"
    android:layout_toLeftOf="@+id/prev_button"
    android:text="start_label" />

  <RelativeLayout
      android:layout_width="wrap_content"
      android:id="@+id/rl"
      android:layout_below="@+id/prev_button"
      android:layout_height="wrap_content"

       >
  </RelativeLayout>

 </RelativeLayout>

活动

public class MainActivity extends Activity {
    private Button btn1;
    private EditText edt1, edt2, edt3, edt4 ,edt5, edt6;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyView mv = new MyView(this);
        setContentView(R.layout.activity_main);
        RelativeLayout layout= (RelativeLayout) findViewById(R.id.rl);
        layout.addView(mv);
    }
    class MyView extends View
    {

        public MyView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }

        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);
            canvas.drawColor(Color.RED);
        }

    }
}

快照

enter image description here

答案 1 :(得分:1)

在尝试查找相对布局之前,您需要对视图进行充气。试试:

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

    //LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //view = mInflater.inflate(R.layout.activity_new_game, gameview, true);

    layout.addView(gameview);

}

答案 2 :(得分:0)

RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout);

您无法在findViewById之前致电setContentView。移动

RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout);
setContentView(R.layout.activity_new_game);

之后

答案 3 :(得分:0)

您没有在代码中初始化layout,因此首先在layout之后初始化setContentView,然后将视图添加到layout

RelativeLayout layout;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_game);
    //Initialize game view
    GameView  gameview=new GameView(this);
    //initialize layout
    layout = (RelativeLayout) findViewById(R.id.relative_layout);
    //adding game view to layout
    layout.addView(gameview);
}

编辑:

setContentView

之前初始化
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout);

是错误的方法,您必须仅在setContentView之后初始化视图,如上面的代码