如何在没有'setContentView(R.layout.main)的情况下创建活动

时间:2012-06-21 22:26:07

标签: android layout views

我知道可以通过执行类似下面的代码来创建活动,其中视图不是从xml文件设置的,而是像这样:setContentView(new myView(this));

我不明白的是如何使用此代码但仍然能够自定义它,例如,如果我想在下面的代码中添加一个按钮,我该怎么做,因为我不能简单地添加一个到xml布局可以吗?

对此的任何好的答案都会得到极大的赞赏 提前致谢!

package com.faceapp;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.view.View;

 public class FaceappActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        setContentView(new myView(this));
    }

    private class myView extends View{

     private int imageWidth, imageHeight;
     private int numberOfFace = 5;
     private FaceDetector myFaceDetect; 
     private FaceDetector.Face[] myFace;
     float myEyesDistance;
     int numberOfFaceDetected;

     Bitmap myBitmap;


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

   BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
   BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565; 
   myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.face5,   
      BitmapFactoryOptionsbfo);
   imageWidth = myBitmap.getWidth();
   imageHeight = myBitmap.getHeight();
   myFace = new FaceDetector.Face[numberOfFace];
   myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
   numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace); 

  }

  @Override
  protected void onDraw(Canvas canvas) {
   // TODO Auto-generated method stub

            canvas.drawBitmap(myBitmap, 0, 0, null);

            Paint myPaint = new Paint();
            myPaint.setColor(Color.GREEN);
            myPaint.setStyle(Paint.Style.STROKE); 
            myPaint.setStrokeWidth(3);

            for(int i=0; i < numberOfFaceDetected; i++)
            {
             Face face = myFace[i];
             PointF myMidPoint = new PointF();
             face.getMidPoint(myMidPoint);
    myEyesDistance = face.eyesDistance();
             canvas.drawRect(
               (int)(myMidPoint.x - myEyesDistance),
               (int)(myMidPoint.y - myEyesDistance),
               (int)(myMidPoint.x + myEyesDistance),
               (int)(myMidPoint.y + myEyesDistance),
               myPaint);
            }
  }
    }
}

^^^^^^^^^^^^^^^ 已回答

如何定位按钮和imageview? (理想情况下使用相对布局) 下面的图片显示了我的意思: (忽略图像重新调整大小)

enter image description here 新代码:

package com.test;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class TesttActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        Button button = new Button(this);
        button.setText("Button!");
        layout.addView(button);

        myView custom = new myView(this);
        layout.addView(custom);

        setContentView(layout);
    }

    private class myView extends View{

        private int imageWidth, imageHeight;
        private int numberOfFace = 5;
        private FaceDetector myFaceDetect; 
        private FaceDetector.Face[] myFace;
        float myEyesDistance;
        int numberOfFaceDetected;

        Bitmap myBitmap;


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

      BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
      BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565; 
      myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.face5,   
      BitmapFactoryOptionsbfo);
      imageWidth = myBitmap.getWidth();
      imageHeight = myBitmap.getHeight();
      myFace = new FaceDetector.Face[numberOfFace];
      myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
      numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace); 

     }

     @Override
     protected void onDraw(Canvas canvas) {
      // TODO Auto-generated method stub

               canvas.drawBitmap(myBitmap, 0, 0, null);

               Paint myPaint = new Paint();
               myPaint.setColor(Color.GREEN);
               myPaint.setStyle(Paint.Style.STROKE); 
               myPaint.setStrokeWidth(3);

               for(int i=0; i < numberOfFaceDetected; i++)
               {
                Face face = myFace[i];
                PointF myMidPoint = new PointF();
                face.getMidPoint(myMidPoint);
       myEyesDistance = face.eyesDistance();
                canvas.drawRect(
                  (int)(myMidPoint.x - myEyesDistance),
                  (int)(myMidPoint.y - myEyesDistance),
                  (int)(myMidPoint.x + myEyesDistance),
                  (int)(myMidPoint.y + myEyesDistance),
                  myPaint);
               }
     }
       }
   }

1 个答案:

答案 0 :(得分:16)

您可以将setContentView()传递给任何形式的视图,作为布局的根视图。下面是一个带有Button和myView的动态构建的LinearLayout。

public class Example extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        // Define the LinearLayout's characteristics
        layout.setGravity(Gravity.CENTER);
        layout.setOrientation(LinearLayout.VERTICAL);

        // Set generic layout parameters
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        Button button = new Button(this);
        button.setText("Button!");
        layout.addView(button, params); // Modify this

        myView custom = new myView(this);
        layout.addView(custom, params); // Of course, this too

        setContentView(layout);
    }
}

了解如果将setContentView()传递给ViewGroup,则只能将子视图添加到根视图中;比如RelativeLayout,LinearLayout等。换句话说,你不能这样做:

        myView custom = new myView(this);

        Button button = new Button(this);
        button.setText("Button!");

        custom.addView(button); 
        // Nope! Method "addView()" does not exist for a regular View...

        setContentView(custom);

此外,命名约定表明类名中的每个单词都应该首字母大写。因此myView应该是MyView,至少它使您的代码更容易为其他程序员阅读,编译器将使用正确的颜色突出显示您的类变量。